Matrix with values dependent on row and column.

3 Ansichten (letzte 30 Tage)
Vince Smith
Vince Smith am 6 Apr. 2019
Beantwortet: Star Strider am 6 Apr. 2019
How would I construct a 10 by 10 matrix whose values are defined by the sum of the value's row and column ?

Akzeptierte Antwort

Star Strider
Star Strider am 6 Apr. 2019
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.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by