I was just writing a "kinda-foreach" loop in Matlab and encountered this strange behavior:
I have the matrix A:
A = [
3
9
5
0];
And I want to use a foreach
loop (as explained here) on the 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 the 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
Which is the result I want.
Can anybody explain what's going on here? What's the difference between these two cases?
when you type
A = [
3
9
5
0];
you create a column vector. Because Matlab iterates over columns you get one answer (the first column). By transposing it you get a row vector with 4 columns and therefore 4 answers each with one column.
In Matlab, the for
loop iterates over columns. http://www.mathworks.es/es/help/matlab/ref/for.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With