필터 지우기
필터 지우기

Change matrix dimensions from 4D to 2 D : matrix manipulation

조회 수: 12 (최근 30일)
Amine Ben Ayara
Amine Ben Ayara 2016년 12월 12일
답변: Kenny Kim 2016년 12월 13일
Dear Matlab community, I am still trying to manipulate some of my output here by converting 4D matrices to 2D. I am having a hard time using "squeeze" and "reshape". I have this matrix (5by5 by 14680 by 30)[ this is basically 30 sets of 14680 of 5by5 matrices), and I need to extract only all the 1st columns from each 5by5 matrices, to have 14680 of (5*1) arrays/matrices, then transpose the (5*1)' so my final matrix has the following dimensions : Final (14680by5) two dimensions only with each row(:,1:5) has the elements from the extracted (5*1) arrays from the step before. I would really appreciate any help! thank you matlab community! Amine
  댓글 수: 3
Matt J
Matt J 2016년 12월 12일
It would also be good to mention in what way this post is not redundant with respect to
Amine Ben Ayara
Amine Ben Ayara 2016년 12월 13일
Kenny & Matt, Sorry guys, I did make a mistake. So starting with a 4D matrix(5*5*14680*30), I need to first extract only the first column from every 5*5 matrix from within the bigger one. Then basically transpose that column to become (1*5) and stack all of the 14680 of them horizontally, then vertically across the last dimension so the final matrix size is on 2D as (14680 by 150) , where the number of columns 150=5*30 I hope I was more clear this time and sorry for the confusion. Kind Regards, Amine

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

채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 12일
Output = squeeze( mat2cell( permute(YourMatrix,[3 1 2 4]), size(YourMatrix,3), size(YourMatrix,1), ones(1,size(YourMatrix,2)), ones(1,size(YourMatrix,4)) ) );
Output will now be a 5 x 30 cell array in which each element is a 14680 x 5 array formed by extracting first columns and making them rows.
  댓글 수: 2
Amine Ben Ayara
Amine Ben Ayara 2016년 12월 12일
Hello Mr. Roberson, Thank you so much for the feedback. I ran your code and I got an output in form of 5by30 cell. I am not really familiar with the way it is produced as I only understand simpler dimensions and commands. Would you be kind enough to look at the screen-shot that I just took of the output and tell me if I did something wrong, or perhaps if I did not explain myself clearly. So suppose that my starting matrix is MS(5*5*14680*30) ( that is 14680 of 5 by 5 matrices and 30 sets of them, from my understanding). Now I need to extract only the first column from each (5by5) matrix from within the bigger matrix MS. Once that is done, I need to convert that column to a row (by transpose I think) so it becomes (1by5) and that is 14680 from within each set, so the final matrix output is then:(14680 by 150) ( two dimensions) I am assuming there is a way to stack all the 30 sets horizontally, hence (5 columns times 30= 150 columns ) Sorry, I must have not explained correctly earlier,
Walter Roberson
Walter Roberson 2016년 12월 13일
Output = reshape( permute( YourMatrix(:,1,:,), [3 1 4 2]), size(YourMatrix,3), []);

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

추가 답변 (1개)

Kenny Kim
Kenny Kim 2016년 12월 13일
This should make the matrix you were looking for:
datatemp = permute(squeeze(data(:,1,:,:)),[2 1 3]);
datatemp = reshape(datatemp,[size(datatemp,1),size(datatemp,2)*size(datatemp,3)]);
where data is the variable name for your 4D matrix.

카테고리

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