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

341,305

Solution 1

You can use linear indexing to access each element.

for idx = 1:numel(array)
    element = array(idx)
    ....
end

This is useful if you don't need to know what i,j,k, you are at. However, if you don't need to know what index you are at, you are probably better off using arrayfun()

Solution 2

As pointed out in a few other answers, you can iterate over all elements in a matrix A (of any dimension) using a linear index from 1 to numel(A) in a single for loop. There are also a couple of functions you can use: arrayfun and cellfun.

Let's first assume you have a function that you want to apply to each element of A (called my_func). You first create a function handle to this function:

fcn = @my_func;

If A is a matrix (of type double, single, etc.) of arbitrary dimension, you can use arrayfun to apply my_func to each element:

outArgs = arrayfun(fcn, A);

If A is a cell array of arbitrary dimension, you can use cellfun to apply my_func to each cell:

outArgs = cellfun(fcn, A);

The function my_func has to accept A as an input. If there are any outputs from my_func, these are placed in outArgs, which will be the same size/dimension as A.

One caveat on outputs... if my_func returns outputs of different sizes and types when it operates on different elements of A, then outArgs will have to be made into a cell array. This is done by calling either arrayfun or cellfun with an additional parameter/value pair:

outArgs = arrayfun(fcn, A, 'UniformOutput', false);
outArgs = cellfun(fcn, A, 'UniformOutput', false);

Solution 3

One other trick is to use ind2sub and sub2ind. In conjunction with numel and size, this can let you do stuff like the following, which creates an N-dimensional array, and then sets all the elements on the "diagonal" to be 1.

d = zeros( 3, 4, 5, 6 ); % Let's pretend this is a user input
nel = numel( d );
sz = size( d );
szargs = cell( 1, ndims( d ) ); % We'll use this with ind2sub in the loop
for ii=1:nel
    [ szargs{:} ] = ind2sub( sz, ii ); % Convert linear index back to subscripts
    if all( [szargs{2:end}] == szargs{1} ) % On the diagonal?
        d( ii ) = 1;
    end
end

Solution 4

You could make a recursive function do the work

  • Let L = size(M)
  • Let idx = zeros(L,1)
  • Take length(L) as the maximum depth
  • Loop for idx(depth) = 1:L(depth)
  • If your depth is length(L), do the element operation, else call the function again with depth+1

Not as fast as vectorized methods if you want to check all the points, but if you don't need to evaluate most of them it can be quite a time saver.

Solution 5

these solutions are more faster (about 11%) than using numel;)

for idx = reshape(array,1,[]),
     element = element + idx;
end

or

for idx = array(:)',
    element = element + idx;
end

UPD. tnx @rayryeng for detected error in last answer


Disclaimer

The timing information that this post has referenced is incorrect and inaccurate due to a fundamental typo that was made (see comments stream below as well as the edit history - specifically look at the first version of this answer). Caveat Emptor.

Share:
341,305
rlbond
Author by

rlbond

I like C++. I also like Python.

Updated on August 14, 2020

Comments

  • rlbond
    rlbond almost 4 years

    I have a problem. I need to iterate through every element in an n-dimensional matrix in MATLAB. The problem is, I don't know how to do this for an arbitrary number of dimensions. I know I can say

    for i = 1:size(m,1)
        for j = 1:size(m,2)
            for k = 1:size(m,3)
    

    and so on, but is there a way to do it for an arbitrary number of dimensions?

  • gnovice
    gnovice about 15 years
    I can't quite see how that ordering of loops will iterate over all elements of a matrix. For example, if you have a 3-by-4 matrix (with 12 elements), your inner loop will only iterate 7 times.
  • Erich Mirabal
    Erich Mirabal about 15 years
    it should iterate over each dimension of the matrix. THe outer loop iterates over the dimension, the inner loop over the size of that dimension. At least, that's the idea. As everyone else is stating, if all he wants is each cell, liner indexing is best. If he wants to iterate over each dimension, he'll have to do something similar to this.
  • Erich Mirabal
    Erich Mirabal about 15 years
    also, thanks for editing. my link was kinda convoluted and just would not work right using the usual linking way. Also, to expand my statement: he would still have to do a lot of other tracking of the index (using like a counter or something like that). I think you or Andrew's approach would be easier for what I think he is trying to do.
  • Phillip Cloud
    Phillip Cloud almost 12 years
    +1 for showing a good example of how MATLAB breaks duck typing.
  • knedlsepp
    knedlsepp over 9 years
    Also, if you wanted to recover the indices for some reason, you still could using these two simple commands: I = cell(1, ndims(array)); [I{:}] = ind2sub(size(array),idx);.
  • rayryeng
    rayryeng about 9 years
    1 : array(:) is equivalent to 1 : array(1). This doesn't iterate through all of elements which is why your run-times are quick. Moreover, rand generates floating-point numbers, and so doing 1 : array(:) would produce an empty array as your statement is trying to find an increasing vector with its initial value as 1 with an ending value as a floating point number with a range of [0,1) exclusive of 1 in increasing steps of 1. There is no such possible vector, which results in an empty vector. Your for loop doesn't run, and so your claim is false. -1 vote. sorry.
  • mathcow
    mathcow about 9 years
    @rayryeng you are not right. array(:) is not equivalent to 1 : array(1). It likes reshape(...).
  • mathcow
    mathcow about 9 years
    @rayryeng matlab r2013a + linux - it works! ;) I just ran that code too
  • rayryeng
    rayryeng about 9 years
    Type on 1 : array(:) in your command prompt after creating array . Do you get an empty matrix? if yes then your code doesn't work. I'm leaving my vote because you are giving false information.
  • mathcow
    mathcow about 9 years
    @rayryeng i'm understanding! yes, you are right, sorry for foolish dispute
  • rayryeng
    rayryeng about 9 years
    That's fine. Now your answer is correct.... Though not very different from the other answers. I'm removing my downvote.