Export 2 Dimension out of a 3 dimensionoal Array

조회 수: 1 (최근 30일)
Mouse
Mouse 2021년 11월 8일
댓글: Star Strider 2021년 11월 8일
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?

답변 (1개)

Star Strider
Star Strider 2021년 11월 8일
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
Mouse
Mouse 2021년 11월 8일
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 2021년 11월 8일
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
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by