Clear Filters
Clear Filters

Export 2 Dimension out of a 3 dimensionoal Array

1 view (last 30 days)
Hi
I hava a 3D Array. Now I like to save the content of the 3d array in n 2d arrays
2dArray = zeros(size(3dArray([1],:,:)));
for z = 1: size(3dArray(:)
2dArray(z)=3dArray(z,:,:)
end
What am I doing wrong?

Answers (1)

Star Strider
Star Strider on 8 Nov 2021
I am not certain what is the desired result.
Usually, the intent is to separate the ‘pages’ (third dimension) of the array into separate 2D arrays.
Likely the best option for that is —
Array3D = randi(9, 3, 4, 4);
Array3D(:,:,2) % Display Page (Dleete Later)
ans = 3×4
2 8 7 2 8 2 6 4 1 9 3 6
A3dsz = size(Array3D)
A3dsz = 1×3
3 4 4
Arrays2D = mat2cell(Array3D, A3dsz(1), A3dsz(2), ones(1,A3dsz(3)));
Arrays2D{2} % Display Result
ans = 3×4
2 8 7 2 8 2 6 4 1 9 3 6
Make appropriate changes to get the desired result.
.
  9 Comments
Mouse
Mouse on 8 Nov 2021
data3D=rand(3,3883,280); %create a 3D array
for i = 1: 3 %now separate every page of the 3D array into 2D arrays
oneDataSet = data3D(i,:,:); %cut away a page "slide"
data_2D=squeeze(oneDataSet);%put away the information that this is a page. Only Information about 2D Array will stay
end
this is my solution to convert a 3 Dimensional Array
Star Strider
Star Strider on 8 Nov 2021
The code in Comment overwrites ‘data_2D’ in every loop iteration. Only one (the last) 2D matrix created will result.
My code produces 3 separate matrices as cell arrays.
A small addition to it will squeeze all of them as well —
Array3D = randi(9, 3, 5, 4);
Array3D(2,:,:); % Display Page (Delete Later)
squeeze(Array3D(2,:,:)) % Display Page (Delete Later)
ans = 5×4
6 9 2 9 7 8 5 8 8 1 8 7 5 7 1 8 6 1 7 6
A3dsz = size(Array3D);
Arrays2D = mat2cell(Array3D, ones(1,A3dsz(1)), A3dsz(2), A3dsz(3));
Arrays2D{2}; % Display Result
Arrays2D = cellfun(@squeeze, Arrays2D, 'Unif',0); % Display Result
Arrays2D{2}
ans = 5×4
6 9 2 9 7 8 5 8 8 1 8 7 5 7 1 8 6 1 7 6
.

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!