87

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
  • 125,304
  • 15
  • 256
  • 359
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • 13
    Matlab terminology note: Matlab has a small number of core data types. The most important are: struct, matrix, and cell array. When referring to parts of a matrix, it's common to use the term "element", and reserve the term "cell" to refer to parts of a cell array. Cell arrays and matrices have numerous syntactic and semantic differences, even though both are N-dimensional data structures. – Mr Fooz Apr 17 '09 at 19:53
  • 3
    May I ask what you need the iteration for? Maybe there is a "vectorized" way to do it instead... – Hosam Aly Nov 09 '09 at 22:39

8 Answers8

93

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()

Andrew
  • 1,167
  • 7
  • 5
  • 1
    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);`. – knedlsepp Jan 04 '15 at 01:23
36

The idea of a linear index for arrays in matlab is an important one. An array in MATLAB is really just a vector of elements, strung out in memory. MATLAB allows you to use either a row and column index, or a single linear index. For example,

A = magic(3)
A =
     8     1     6
     3     5     7
     4     9     2

A(2,3)
ans =
     7

A(8)
ans =
     7

We can see the order the elements are stored in memory by unrolling the array into a vector.

A(:)
ans =
     8
     3
     4
     1
     5
     9
     6
     7
     2

As you can see, the 8th element is the number 7. In fact, the function find returns its results as a linear index.

find(A>6)
ans =
     1
     6
     8

The result is, we can access each element in turn of a general n-d array using a single loop. For example, if we wanted to square the elements of A (yes, I know there are better ways to do this), one might do this:

B = zeros(size(A));
for i = 1:numel(A)
  B(i) = A(i).^2;
end

B
B =
    64     1    36
     9    25    49
    16    81     4

There are many circumstances where the linear index is more useful. Conversion between the linear index and two (or higher) dimensional subscripts is accomplished with the sub2ind and ind2sub functions.

The linear index applies in general to any array in matlab. So you can use it on structures, cell arrays, etc. The only problem with the linear index is when they get too large. MATLAB uses a 32 bit integer to store these indexes. So if your array has more then a total of 2^32 elements in it, the linear index will fail. It is really only an issue if you use sparse matrices often, when occasionally this will cause a problem. (Though I don't use a 64 bit MATLAB release, I believe that problem has been resolved for those lucky individuals who do.)

  • Indexing in 64-bit MATLAB does indeed correctly allow 64-bit subscripts. For example: `x = ones(1,2^33,'uint8'); x(2^33)` works as expected. – Edric Nov 19 '12 at 09:38
  • @Edric - Of course, this is a behavior that surely would have changed in the years (and many releases) since I made that statement. Thanks for checking though. –  Nov 19 '12 at 12:54
  • :) I didn't realise how old the answer was until after I commented - the question just showed up in my RSS feed, and I didn't even notice that I'd answered it too! – Edric Nov 19 '12 at 13:01
15

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);
gnovice
  • 125,304
  • 15
  • 256
  • 359
13

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
Edric
  • 23,676
  • 2
  • 38
  • 40
1

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.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
1

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.

Community
  • 1
  • 1
mathcow
  • 71
  • 2
  • 1
    `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. – rayryeng Apr 09 '15 at 03:02
  • @rayryeng you are not right. array(:) is not equivalent to 1 : array(1). It likes `reshape(...)`. – mathcow Apr 09 '15 at 07:18
  • @rayryeng matlab r2013a + linux - it works! ;) I just ran that code too – mathcow Apr 09 '15 at 08:53
  • 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. – rayryeng Apr 09 '15 at 12:42
  • @rayryeng i'm understanding! yes, you are right, sorry for foolish dispute – mathcow Apr 09 '15 at 12:56
  • That's fine. Now your answer is correct.... Though not very different from the other answers. I'm removing my downvote. – rayryeng Apr 09 '15 at 13:34
-1

You want to simulate n-nested for loops.

Iterating through n-dimmensional array can be seen as increasing the n-digit number.

At each dimmension we have as many digits as the lenght of the dimmension.

Example:

Suppose we had array(matrix)

int[][][] T=new int[3][4][5];

in "for notation" we have:

for(int x=0;x<3;x++)
   for(int y=0;y<4;y++)
       for(int z=0;z<5;z++)
          T[x][y][z]=...

to simulate this you would have to use the "n-digit number notation"

We have 3 digit number, with 3 digits for first, 4 for second and five for third digit

We have to increase the number, so we would get the sequence

0 0 0
0 0 1
0 0 2    
0 0 3
0 0 4
0 1 0
0 1 1
0 1 2
0 1 3
0 1 4
0 2 0
0 2 1
0 2 2
0 2 3
0 2 4
0 3 0
0 3 1
0 3 2
0 3 3
0 3 4
and so on

So you can write the code for increasing such n-digit number. You can do it in such way that you can start with any value of the number and increase/decrease the digits by any numbers. That way you can simulate nested for loops that begin somewhere in the table and finish not at the end.

This is not an easy task though. I can't help with the matlab notation unfortunaly.

bmegli
  • 7
  • 1
-1

If you look deeper into the other uses of size you can see that you can actually get a vector of the size of each dimension. This link shows you the documentation:

www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html

After getting the size vector, iterate over that vector. Something like this (pardon my syntax since I have not used Matlab since college):

d = size(m);
dims = ndims(m);
for dimNumber = 1:dims
   for i = 1:d[dimNumber]
      ...

Make this into actual Matlab-legal syntax, and I think it would do what you want.

Also, you should be able to do Linear Indexing as described here.

gnovice
  • 125,304
  • 15
  • 256
  • 359
Erich Mirabal
  • 9,860
  • 3
  • 34
  • 39
  • 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. – gnovice Apr 17 '09 at 04:34
  • 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 Apr 17 '09 at 14:22
  • 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. – Erich Mirabal Apr 17 '09 at 20:04