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

96 次查看(过去 30 天)
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