Taking 3-D matrices out of a 4-D matrix

36 views (last 30 days)
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!!

Accepted Answer

Stephen23
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
Sarah
Sarah on 5 Mar 2020
Hi Stephen,
Yes, the ultimate goal is to not number the vairables, which is why I said I wanted to avoid that.
In regards to turning it into a cell, I do not have much experience with cells. 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 (now relabeled as a cell)? I need each 2000 by 2000 1-D matrix to become a sizw 200 by 200 1-D matrix so that the ending dimensions are 200 by 200 by 3 by 4.
In using num2cell, what is the 1:3 referencing? (from: C = squeeze(num2cell(A,1:3))
And can you explain why you are applying the squeeze function? I see the difference, but now sure how it is working on a cell.
Thanks!
Stephen23
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.

Sign in to comment.

More Answers (1)

James Tursa
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.
  1 Comment
Sarah
Sarah on 5 Mar 2020
Hi James,
As I mentioned below, hard-coding in is possible, but I am trying to avoid it so that I can allow for future changes in the number in the 4th dimension.
To be clear, the following code is what I am trying to achieve because I need to use imresize, but there will not always be 4 3-D matrices. There will likely be more or less, and I would rather not have to change the hard-coded code.
B1=A(:,:,:,1);
B2=A(:,:,:,2);
B3=A(:,:,:,3);
B4=A(:,:,:,4);
B1_new=imresize(B1, 0.1);
B2_new=imresize(B2, 0.1);
B3_new=imresize(B3, 0.1);
B4_new=imresize(B4, 0.1);

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!