MATLAB: Looping through a matrix of unknown dimensions

for loopmatrix manipulation

Hi everyone, I know similar questions have been asked before but I can't find anything that would be relevant for my particular situation. I need to write a function that would retrieve a vector from a matrix of unknown size, operate on it, and then return the results into an output matrix of size equal to the starting matrix. Here is an example of what I need:
start_matrix = rand(5,6,7,8,1000);
s = size(start_matrix);
output_matrix = zeros(s);
for a = 1:s(1)
for b = 1:s(2)
for c = 1:s(3)
for d = 1:s(4)
operate_data = start_matrix(a,b,c,d,:); % retrieving the vector of length=1000
outputmatrix(a,b,c,d,:) = fft(operate_data); % using fast fourier transform as an example, there are a few more calculations here
end
end
end
end
Now the number of dimensions can vary and the size of them can vary too, meaning that the number of loops needed will be variable. So my question is, how do I make this work with e.g. size = [10,15,20,500], where I loop through the first three dimensions, doing calculations on vectors of length = 500?
I've been trying to implement something like in this example by Jan:
But I don't know how to make it work when I am not operating on each element, but rather a vector, each time.
Help is greatly appreciated! Thanks.

Best Answer

What about reshaping the array?
start_matrix = rand(5,6,7,8,1000);
s = size(start_matrix);
M = reshape(start_matrix, [], s(end));
Mfft = fft(M, [], 2);
Result = reshape(Mfft, s);
isequal(Result, output_matrix)
>> logical(1)
This is much faster than a loop. But if you really need a set of loop with distinct dimensions, this is the way to go:
X = rand(5,6,7,8,1000);
Out = zeros(size(X)); % Pre-allocate the output
% Prepare index vector as cell, e.g. {1,2,3,4,':'} :
nv = ndims(X) - 1; % Exclude last dimension
v = [repmat({1}, 1, nv), {':'}];
vLim = size(X); % Loop until size (last element not used)
ready = false;
while ~ready
% The actual operation:
Out(v{:}) = fft(X(v{:}));
% Update the index vector:
ready = true; % Assume that the WHILE loop is ready
for k = 1:nv % Loop through dimensions
v{k} = v{k} + 1; % Increase current index by 1
if v{k} <= vLim(k) % Does it exceed the length of current dim?
ready = false; % No, WHILE loop is not ready now
break; % v(k) increased successfully, leave "for k" loop
end
v{k} = 1; % Reset v{k}, proceed to next k
end
end
The idea is to create a cell vector, which contains the indices, e.g. {1,2,3,4,':'}. Then this is equivalent:
index = {1, 2, 3, 4, ':'}
A(1,2,3,4,:)
A(index{:})
Now tis index vector is used and updated in the for loop: The first element is increased by 1. If it does not exceed size(X, 1), the next iteration is performed. If it exceeds size(X, 1), the element is reset to 1 and the next one is increased.