Description. B = repmat( A , n ) returns an array containing n copies of A in the row and column dimensions. The size of B is size(A)*n when A is a matrix. B = repmat( A , r1,...,rN ) specifies a list of scalars, r1,..,rN , that describes how copies of A are arranged in each dimension.
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.
for i=1:length(list)
elm = list(i);
//do something with elm.
with many functions in matlab, you don't need to iterate at all.
for example, to multiply by it's position in the list:
m = [1:numel(list)]';
elm = list.*m;
vectorized algorithms in matlab are in general much faster.
If you just want to apply a function to each element and put the results in an output array, you can use arrayfun
.
As others have pointed out, for most operations, it's best to avoid loops in MATLAB and vectorise your code instead.
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