Azzera filtri
Azzera filtri

Matrix with values dependent on row and column.

1 visualizzazione (ultimi 30 giorni)
How would I construct a 10 by 10 matrix whose values are defined by the sum of the value's row and column ?

Risposta accettata

Star Strider
Star Strider il 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.

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by