How can I make the numbers count horizontal instead of vertical?

2 vues (au cours des 30 derniers jours)
Tyler Mead
Tyler Mead le 10 Nov 2019
Commenté : Tyler Mead le 11 Nov 2019
I need to figure out how to change my final array.
a = input('What is the size of the square array? (Enter one number): ','s');
sq = str2num(a);
elements = 0;
for x = 1:sq
elements = x + elements;
end
A = zeros(sq);
c = 0;
for i = 1:sq
for i2 = 1:i
c = c + 1;
A(i2, i) = c;
end
end
disp(A)
If I enter the number 6, my array will look like this:
1 2 4 7 11 16
0 3 5 8 12 17
0 0 6 9 13 18
0 0 0 10 14 19
0 0 0 0 15 20
0 0 0 0 0 21
How can I get this array to look like this instead?
1 2 3 4 5 6
0 7 8 9 10 11
0 0 12 13 14 15
0 0 0 16 17 18
0 0 0 0 19 20
0 0 0 0 0 21
  2 commentaires
Walter Roberson
Walter Roberson le 11 Nov 2019
A(i, i2) = c;
perhaps ?
Tyler Mead
Tyler Mead le 11 Nov 2019
Got it fixed, but that wouldn't work either. Thanks for the feedback though.

Connectez-vous pour commenter.

Réponse acceptée

Shubham Gupta
Shubham Gupta le 11 Nov 2019
Try
a = input('What is the size of the square array? (Enter one number): ','s');
sq = str2num(a);
elements = 0;
for x = 1:sq
elements = x + elements;
end
A = zeros(sq);
c = 0;
for i = 1:sq
for i2 = i:sq
c = c + 1;
A(i,i2) = c;
end
end
disp(A)

Plus de réponses (0)

Catégories

En savoir plus sur Language Fundamentals 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