reshape 4D matrix into 2D matrix

64 ビュー (過去 30 日間)
Amine Ben Ayara
Amine Ben Ayara 2016 年 10 月 18 日
コメント済み: James 2016 年 10 月 19 日
Good morning Matlab, I have a 4D-Double matrix that has this dimension: (5*5*5*30) and I need to convert it into a 2D matrix, I suppose, so the new matrix will only have the 1st Column (:,1) from each five by five matrix stacked horizontally,so the new 2D matrix should be 25 by 30 (25*30) dimension, where each column (:,1:30) is basically extracted from the original matrix (5*5*5*30) matrix. To try to explain this a bit more, I have 30 of: five sets of (5by5) matrices, and I only need the information in the first column of each set of 5*5, hence (25 rows) and 30 columns. Can Someone pleaaaaase point me in the right direction? Thank you SO much! have a great day.

回答 (2 件)

James
James 2016 年 10 月 18 日
Does this do what you're looking for?
reshape(squeeze(x(:,1,:,:)),25,30)
where x is your 5x5x5x30 matrix?
  2 件のコメント
Amine Ben Ayara
Amine Ben Ayara 2016 年 10 月 18 日
hello James, Thank you so much for the help. It does look like what I need, which from what you wrote so far the output matrix is of dimension: 25 rows by 30 columns, right? I tried it and I got this error: Error using reshape To RESHAPE the number of elements must not change.
James
James 2016 年 10 月 19 日
Try reshape(squeeze(x(:,1,:,:)),25,[]), and see what the final size of the matrix is.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2016 年 10 月 18 日
Possibly
reshape(M(:, 1, :, :), 25, 30)
Or
reshape( permute( M(:, 1, :, :), [3, 1, 4, 2]), 25, 30)
The difference is that for an A x B x C x D matrix the first would be (A*C) x D, but the second would be (C*A) x D different of elements
  2 件のコメント
Amine Ben Ayara
Amine Ben Ayara 2016 年 10 月 18 日
Hello Walter, Thank you so much for the reply. I tried both methods and I got this: A=reshape(MNPr1_1to30(:, 1, :, :), 25, 30) ; Error using reshape To RESHAPE the number of elements must not change.
>> AAA=reshape( permute( MNPr1_1to30(:, 1, :, :), [3, 1, 4, 2]), 25, 30); Error using reshape To RESHAPE the number of elements must not change.
Walter Roberson
Walter Roberson 2016 年 10 月 18 日
Please show size( MNPr1_1to30)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by