Matlab: foreach element in array; do something.

This entry was posted by on Thursday, 27 October, 2011 at

If you have your data in an array (or matrix for that matter) and want to perform operations on it, most programming languages have foreach statement which lets you easily iterate all values. Matlab, actually, does not differ much.

Say you have an array of data:

n = [1 2 3 4 6 12 18 51 69 81 ]

then you can ‘foreach’ it like this:

for i = n, i, end

This will echo every element in n (but replacing the i with more interesting stuff is also possible of course!)

Leave a Reply