How to do this by using for loop Help me pls

Answers (1)

Hint: when you are increasing on the first figure, if you are just starting the N'th line, then the number of values "used up" so far is the sum of the positive integers less than N, 1+2+3+4+5...+(N-1). There is a formula for that, which is N*(N-1)/2. For example for the 7th increasing line, 1+2+3+4+5+6 values would have been used up already, which would be 6*7/2 = 21.
So as long as you are increasing, if you are about to start the N'th line, N*(N-1)/2 have already been used up, the last of which would have been 2 times that minus 1, and the next available, the one to start this line with, would be N*(N-1) + 1.
And you are going to outputing N of those on the line, each with a difference of 2. You can therefore directly calculate the first and last to be put on the row: for V=N*(N-1)+(1:2:2*N-1)
You can use similar calculations on the descent.

Asked:

on 4 Feb 2021

Commented:

on 4 Feb 2021

Community Treasure Hunt

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

Start Hunting!