Info

This question is closed. Reopen it to edit or answer.

How can i create the below Matrix most elegantly using two for loops.

1 view (last 30 days)
|1 8 15|
|2 9 16|
|3 10 17|
|4 11 18|
|5 12 19|
|6 13 20|
|7 14 21|
i tried doing:
for i=1:21;
X = i;
for j=1:3 ;
Matrix[] =[X(i) Y(j)];

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 1 Jun 2015
Edited: Azzi Abdelmalek on 1 Jun 2015
reshape(1:21,7,[])
  4 Comments
Stephen23
Stephen23 on 1 Jun 2015
Edited: Stephen23 on 1 Jun 2015
@Shashank Sharma: why do this in two loops? A neater, faster and much more expandable solution is to use Azzi Abdelmalek's original answer. The title of your question is "How can i create the below Matrix most elegantly using two for loops". The most elegant solution is not to use loops, but to use Azzi Abdelmalek's answer.

Community Treasure Hunt

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

Start Hunting!