FOR loop over column vector vs row vector

I just wrote a kinda-foreach loop in Matlab and came across this weird behavior:

I have a matrix A:

A = [ 3 9 5 0]; 

And I want to use the foreach (as described here ) on A.

If I write this:

 for i = A disp('for') i end 

The result will be:

 for i = 3 9 5 0 

But when I use transpose, the result will change:

 for i = A' disp('for') i end 

Result:

 for i = 3 for i = 9 for i = 5 for i = 0 

What result do I want.

Can someone explain what is going on here? What is the difference between the two cases?

+7
for-loop matlab
source share
2 answers

when entering

 A = [ 3 9 5 0]; 

you create a column vector. Since Matlab iterates through the columns, you get one answer (the first column). By rearranging it, you get a row vector with 4 columns and, therefore, 4 responses each with one column.

+6
source share

In Matlab, a for loop iterates through the columns. http://www.mathworks.es/es/help/matlab/ref/for.html

+5
source share

All Articles