MATLAB: How can someone make a 1234,123,12,1 pattern in a triangle

looppatterntriangle

when n=1,how can I do a pattern from 1:n so it printed out like that(using for or while loop)
123456
12345
1234
123
12
1

Best Answer

Here is a start for you. It might be easier to construct the loop using backwards indexing. E.g.,
n = 6;
for k=n:-1:1
% Insert code here for your number
end
So you need to fill in the loop above. E.g., when k=6 at the first iteration, how do you form the number 123456? Then when k=5 in the next iteration, how do you form the number 12345? Etc. Maybe you can think of a vectorized way to do this, or maybe you can think of another loop to do this. If you are stuck with how to code this, try to think of how you might do it by hand first. Then come up with a pattern that you can code up.
Related Question