MATLAB: Matrix with values dependent on row and column.

MATLABmatrix

How would I construct a 10 by 10 matrix whose values are defined by the sum of the value's row and column ?

Best Answer

One approach:
[rm,cm] = ndgrid(1:10);
rv = rm(:);
cv = cm(:);
el = rv + cv;
R = reshape(el, 10, 10) % Desired Result
producing:
R =
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
This could be written in two lines, of course. I wrote it this way to demonstrate how the code works.