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

86 ビュー (過去 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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by