How can I build a matrix with an increasing number of terms in each row?

4 vues (au cours des 30 derniers jours)
Parker Pittman
Parker Pittman le 14 Oct 2021
Modifié(e) : Dave B le 15 Oct 2021
I'm trying to build a matrix which has an increasing number of summed terms in each row.
For example n = 1 would be the first row, the sum of n = 1 and n = 2 in the second row and so forth, all the way up through n = 50.
I believe the matrix should come out like
1
1 2
1 2 3

Réponse acceptée

Dave B
Dave B le 14 Oct 2021
Modifié(e) : Dave B le 15 Oct 2021
You can't have a matrix with a different number of elements on each row, but if you wanted the sums 1, 1+2, 1+2+3,... (as you describe):
n = 1:50;
sums = n.*(n+1)/2;
sums.'
ans = 50×1
1 3 6 10 15 21 28 36 45 55

Plus de réponses (1)

Image Analyst
Image Analyst le 14 Oct 2021
If you're willing to use a cell array instead of a regular matrix, you can do this:
n = 50;
ca = cell(n, n)
for row = 1 : n
for col = 1 : row
ca{row, col} = sum(1:col);
end
end
The "unused" cells will still be there but they will have null inside them.

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by