필터 지우기
필터 지우기

Reshaping a complex 3D array into 1D, and back

조회 수: 46 (최근 30일)
Nick Keepfer
Nick Keepfer 2020년 6월 18일
답변: James Tursa 2020년 6월 18일
I have a 3D complex array of size (nx,ny,nz).
For post-processing purposes I need to convert into a flattened array of size (1,nx*ny*nz)
I then need to convert it back into its 3D form.
The issue here is that the following code destroys the original formatting of the 3D array
1dwave = reshape(3dwave,[nx*ny*nz,1]);
recovered_wave = reshape(1dwave,size(3dwave));
In essence:
1dwave != recovered_wave
Can somebody tell me what the correct way to do this is?
i.e. How can I convert from 3D -> 1D -> 3D whilst preserving shape of the original 3D array
  댓글 수: 2
James Tursa
James Tursa 2020년 6월 18일
편집: James Tursa 2020년 6월 18일
The reshape( ) function does not change the memory order of the elements. What you have should have worked. Can you give a small example where it doesn't work? Are you sure the original size is strictly 3D with dimensions nx x ny x nz?
Nick Keepfer
Nick Keepfer 2020년 6월 18일
That was my suspicion, I was expecting it to work too.
So I have simplified my entire process somewhat, to aid with the understanding of the problem, but let me elaborate.
I have T samples of a 3D (nx,ny,nz) array. I then (each loop iteration) save the flattened version of the 3D array into a row of a matrix
u(T,:) = 1dwave;
Once the loop completes, If I pull out say u(T,1) and attempt to reshape it as specified above, it does not match the input. With no doubt, the original size is strictly 3D with dimensions nx, ny, nz.
Perhaps the act of stacking these samples inside "u" causes some problems in how Matlab reformulates the array.

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

채택된 답변

James Tursa
James Tursa 2020년 6월 18일
I suspect the problem may be that you originally put the data into rows of a matrix. This separates the elements in memory. I.e., MATLAB is column ordered for memory layout, and elements of the same column are next to each other in memory. Elements of the same row are not next to each other in memory in general. Once you put the data into rows you have changed the memory layout of the data, and no amount of reshaping will recover the original memory layout of the data. You would have to probably use the permute( ) function to get back to your desired memory layout.

추가 답변 (1개)

David Hill
David Hill 2020년 6월 18일
1dwave=3dwave(:)';
recovered_wave=reshape(1dwave,size(3dwave));
  댓글 수: 1
Nick Keepfer
Nick Keepfer 2020년 6월 18일
Unfortunately this doesn't work either. Please look at my comment above though which elaborates further

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by