Luis's answer is great for being semi-vectorized (mat2cell
uses a loop). If what you desire is a cell array of size x*y*z
where each element is t
long, it's possible to use a loop over each volume location and extract the t
elements that "temporally" occupy this spot in 4D. Make sure you squeeze
out any singleton dimensions to get the resulting vector. This is something to consider if you wanted to go with the loop approach. Assuming your matrix is called A
, try the following:
B = cell(size(A,1)*size(A,2)*size(A,3), 1);
count = 1;
for ii = 1 : size(A,1)
for jj = 1 : size(A,2)
for kk = 1 : size(A,3)
B{count} = squeeze(A(ii,jj,kk,:));
count = count + 1;
end
end
end
To get this back into a 4D matrix form, you'd just apply the same logic but in reverse:
Ar = zeros(size(A));
count = 1;
for ii = 1 : size(A,1)
for jj = 1 : size(A,2)
for kk = 1 : size(A,3)
Ar(ii,jj,kk,:) = B{count};
count = count + 1;
end
end
end