Taking 3-D matrices out of a 4-D matrix
29 views (last 30 days)
Show older comments
How can I take a 4-D matrix of (for example) size 2000 by 2000 by 3 by 4 and break up the 4-D matrix into 4 separate 3-D matrices with sizes 2000 by 2000 by 3? I would preferably like to use a loop, and will need to store each of the matrices, so something like:
A=rand(2000,2000,3,4);
for i-1:4
B=A(:,:,:,i);
end
will not work since I am overwriting B on each iteration. Any ideas on how to separate out AND store are greatly appreciated.
Thanks!!
0 Comments
Accepted Answer
Stephen23
on 5 Mar 2020
Edited: Stephen23
on 5 Mar 2020
Although most likely you would be better off not splitting up your perfectly good numeric array, if you really want to you could use num2cell, e.g.:
C = squeeze(num2cell(A,1:3))
and then simply access the arrays using very efficient indexing:
Whatever you do, do NOT dynamically name variables for the output arrays:
4 Comments
Stephen23
on 5 Mar 2020
Edited: Stephen23
on 6 Mar 2020
"Can you elaborate more (i.e. provide sample code) on how to use imresize on each 1-D matrix that composes the 3-D and the 4-D"
It is not clear what you are referring to by "1-D matrix". Your original question requested to "... break up the 4-D matrix into 4 separate 3-D matrices..." but did not mention 1D arrays anywhere. It is unclear what you expect to occur when you write "...how to use imresize on each 1-D matrix...", or even what data that refers to.
If you want to resize the 3D arrays in the cell array then use a simple loop:
for k = 1:numel(C)
C{k} = imresize(C{k},...); % replace the ellipsis with your size option/s.
end
"200 by 200 1-D matrix"
An array with size 200 x 200 is 2D, not 1D. I have no idea what your "1-D" statements refer to.
"In using num2cell, what is the 1:3 referencing?"
The num2cell documentation states that the second argument "... dim specifies which dimensions of A to include in each cell", so by specifying dim to be the vector [1,2,3] I told num2cell to keep those dimensions together, and only split the input array along the 4th dimension (which is what you asked for in your original question). Reading the documentation and experimenting is highly recommended.
"And can you explain why you are applying the squeeze function?"
It is entirely optional, the only reason I used it is because the cell array displayed in the command window is neater as a column vector. Without squeeze the output cell array would have size 1x1x1x4, with squeeze it simply becomes 4x1. In both cases you can access the data in exactly the same way using linear indexing.
"I see the difference, but now sure how it is working on a cell."
There is nothing special about squeeze on a cell array, it works exactly the same way on any class of array.
More Answers (1)
James Tursa
on 5 Mar 2020
What is wrong with accessing the 4D array using simple indexing?
A(:,:,:,k) for k'th 3D array.
You could also use cell arrays, but I don't yet see the need.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!