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?
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 from1
tonumel(A)
in a single for loop. There are also a couple of functions you can use:arrayfun
andcellfun
.Let's first assume you have a function that you want to apply to each element of
A
(calledmy_func
). You first create a function handle to this function:If
A
is a matrix (of type double, single, etc.) of arbitrary dimension, you can usearrayfun
to applymy_func
to each element:If
A
is a cell array of arbitrary dimension, you can usecellfun
to applymy_func
to each cell:The function
my_func
has to acceptA
as an input. If there are any outputs frommy_func
, these are placed inoutArgs
, which will be the same size/dimension asA
.One caveat on outputs... if
my_func
returns outputs of different sizes and types when it operates on different elements ofA
, thenoutArgs
will have to be made into a cell array. This is done by calling eitherarrayfun
orcellfun
with an additional parameter/value pair: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):
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.
One other trick is to use
ind2sub
andsub2ind
. In conjunction withnumel
andsize
, 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.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)
in "for notation" we have:
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
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.
You could make a recursive function do the work
L = size(M)
idx = zeros(L,1)
length(L)
as the maximum depthfor idx(depth) = 1:L(depth)
length(L)
, do the element operation, else call the function again withdepth+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.
these solutions are more faster (about 11%) than using
numel
;)or
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.