How to iterate over a column vector in Matlab?

snakile picture snakile · Aug 11, 2010 · Viewed 180.4k times · Source

Possible Duplicate:
How do I iterate through each element in an n-dimensional matrix in MATLAB?

I have a column vector list which I would like to iterate like this:

for elm in list
   //do something with elm

How?

Answer

Jonas picture Jonas · Aug 11, 2010

In Matlab, you can iterate over the elements in the list directly. This can be useful if you don't need to know which element you're currently working on.

Thus you can write

for elm = list
%# do something with the element
end

Note that Matlab iterates through the columns of list, so if list is a nx1 vector, you may want to transpose it.