MATLAB: How can print out the below content by MATLAB program

displaylogic

Hi,
i want to use the MATLAB to print out the below result, but I could not put the values in each row, how can I revise my program?
Thanks!
What I want to print out:
1
21
321
4321
54321
And there are my code & result:
for i = 1:1:5
for j =i:-1:1
disp(j);
end
end
1
2
1
3
2
1
4
3
2
1
5
4
3
2
1

Best Answer

You do not need the second for loop. Simply print the value of ‘j’ in each iteration of the ‘i’ loop:
for i = 1:1:5
j =i:-1:1;
disp(j)
end
producing:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1