How can I convert back correctly a 2D matrix to 3D one?

5 views (last 30 days)
Hi
I converted my 3D matrix A(464 x 201 x 10957) to 2D matrix in matlab by:
B = reshape(A,size(A,3),[],1);
After I convert B(10957,93264) back to 3D matrix as the original (464 x 201 x10957) by:
C=reshape(B,size(B,2),[],1);
D=reshape(C,464,201,[]);
the produced matrix will not to be same as the original one. How can I convert back the 2D matrix correctly?
Thank you for your help in advance! Beata

Answers (1)

David Young
David Young on 14 Aug 2015
Edited: David Young on 14 Aug 2015
First, the resulting array will be the same as the original. Here's a demonstration (making the arrays rather smaller to avoid clobbering memory):
A = rand(464, 201, 57); % demo data
B = reshape(A,size(A,3),[],1);
C=reshape(B,size(B,2),[],1);
D=reshape(C,464,201,[]);
isequal(A, D) % prints 1
Second, you don't need to go via C. Thus
D = reshape(B,464,201,[]);
also makes D the same as A.
But third, what you're doing doesn't look sensible. I can't think why you'd want to give B's first dimension the same size as A's third dimension. Remember that reshape does not change the order of the elements in the underlying vector - it just divides it up differently amongst the dimensions. So B and C are likely to be jumbled up, unless somehow the original structure of A is very special. I wonder if what you really want is permute.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!