Effacer les filtres
Effacer les filtres

Matrix with values dependent on row and column.

1 vue (au cours des 30 derniers jours)
Vince Smith
Vince Smith le 6 Avr 2019
How would I construct a 10 by 10 matrix whose values are defined by the sum of the value's row and column ?

Réponse acceptée

Star Strider
Star Strider le 6 Avr 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.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by