Using "for" to create loops

I am trying to create a loop to have a (ixm)x2 matrix such as:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
My formulation is as follows:
i = size(input,1);
m = max(input(:,5));
for indice = 1:i;
for indice2 = 1:m;
Zassign = (1,[indice, indice2]);
end
end
I am missing something in here. If you may help me, I would really appreciate it.

1 Comment

If possible, can I also write a matrix as following:
Z 1 1
Z 1 2
Z 1 3
Z 2 1
Z 2 2
Z 2 3
Z 3 1
Z 3 2
Z 3 3
I am a new MATLAB user. I know MATLAB deals with numbers. However, I just wanted to ask to make sure if that is not possible.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 6 Jul 2016
Edited: Stephen23 on 6 Jul 2016
Why waste time with a loop? Code vectorization is much more beautiful!
>> [X,Y] = meshgrid(1:3);
>> mat = [X(:),Y(:)]
mat =
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
And if you really want the Z's you can print them, but they cannot be stored in a numeric matrix (unless you store the character code):
>> fprintf(' Z %d %d\n',mat.')
Z 1 1
Z 1 2
Z 1 3
Z 2 1
Z 2 2
Z 2 3
Z 3 1
Z 3 2
Z 3 3

7 Comments

Taner Cokyasar
Taner Cokyasar on 6 Jul 2016
Edited: Taner Cokyasar on 6 Jul 2016
It definitely works Stephen. Thank you so much for those. I have one more question. What if I want to add a column vector at the end of mat. The column vector I am talking is the result of my question. I just want to show results matched with its corresponding variables.
Let's say:
Z 1 1 0
The zero at the end represents the answer for the variable Z11. I have a column vector of answers for each variable named as "Zvalues".
You can use much the same idea. If the results are in a vector Z, then simply concatenate it onto the back:
>> Z = 1:9;
>> [X,Y] = meshgrid(1:3);
>> mat = [X(:),Y(:),Z(:)]
mat =
1 1 1
1 2 2
1 3 3
2 1 4
2 2 5
2 3 6
3 1 7
3 2 8
3 3 9
and for printing you just need to add the appropriate formatting operator to the fprintf string:
>> fprintf(' Z %d %d %d\n',mat.')
You are great Stephen! "%d" was what I was missing. God Bless!
@Taner Cokyasar: you should read the fprintf documentation carefully, because there are a lot of options for these formatting operators. Practice and try them to pick one that best suits your needs.
Taner Cokyasar
Taner Cokyasar on 6 Jul 2016
Edited: Taner Cokyasar on 6 Jul 2016
Actually meshgrid is not working for this situation. I just chose a lucky example by using i=3 and m=3. When I tried i=10 and m=3, it doesn't work. Do you have any idea? I mean:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
.
.
.
10 1
10 2
10 3
Did you read the meshgrid documentation ? You can use two inputs to meshgrid:
>> [X,Y] = meshgrid(1:10,1:3);
>> mat = [X(:),Y(:)]
mat =
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
5 1
5 2
5 3
6 1
6 2
6 3
7 1
7 2
7 3
8 1
8 2
8 3
9 1
9 2
9 3
10 1
10 2
10 3
After this answer, yes :) I appreciate it.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!