Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Matrix using Matrix Indices

Tags:

matlab

In matlab, how could you create a matrix M using its indices to populate the values? For example, say I want to create a 3x3 matrix M such that

M(i,j) = i+j --> [ 2 3 4; 3 4 5; 4 5 6]

I tried making vectors: x = 1:3', y = 1:3 and then

M = x(:) + y(:)

but it didn't work as expected.

Any thoughts on how this can be done?

Thanks!

UPDATE

The M I actually desire is:

M(i,j) = -2^(-i - j).

1 Answers

You should use bsxfun to find the sum:

M=bsxfun(@plus, (1:3).', 1:3)

and for the second formula:

M=-2.^(-bsxfun(@plus, (1:3).', 1:3))
like image 169
Mohsen Nosratinia Avatar answered Jul 28 '26 08:07

Mohsen Nosratinia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!