Hello, Im trying to convert a 3d matrix x(9,9,625) into a 2d x2(225,225) .
I want to put each (:,:,i) of x in order for the first 25 chunks and then change 'col' in the 3d.
for example if x (:,:,1) was [1,2,3 x(:,:,2) = [7,8,9 x(:,:,3) = [13,14,15 x(:,:,4) = [19,20,21
4,5,6] 10,11,12] 16,17,18] 22,23,24]
then x2 would be x2 = [1,2,3, 7,8,9
4,5,6, 10,11,12
13,14,15 ,19,20,21
16,17,18 , 22,23,24]
if we split it for each 2 chunks.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 11일
편집: Ameer Hamza 2020년 4월 11일

0 개 추천

% Example matrix
x(:,:,1) = [1 2 3; 4 5 6];
x(:,:,2) = [1 2 3; 4 5 6]+6;
x(:,:,3) = [1 2 3; 4 5 6]+12;
x(:,:,4) = [1 2 3; 4 5 6]+18;
n = sqrt(size(x,3));
x2 = mat2cell(x, size(x,1), size(x,2), ones(1,size(x,3)));
x2 = reshape(x2,n,n)';
x2 = cell2mat(x2);
x2 =
1 2 3 7 8 9
4 5 6 10 11 12
13 14 15 19 20 21
16 17 18 22 23 24
Example for another matrix x
% Example matrix
x(:,:,1) = [1 2 3; 4 5 6; 7 8 9];
x(:,:,2) = [1 2 3; 4 5 6; 7 8 9]+9;
x(:,:,3) = [1 2 3; 4 5 6; 7 8 9]+18;
x(:,:,4) = [1 2 3; 4 5 6; 7 8 9]+27;
x(:,:,5) = [1 2 3; 4 5 6; 7 8 9]+36;
x(:,:,6) = [1 2 3; 4 5 6; 7 8 9]+45;
x(:,:,7) = [1 2 3; 4 5 6; 7 8 9]+54;
x(:,:,8) = [1 2 3; 4 5 6; 7 8 9]+63;
x(:,:,9) = [1 2 3; 4 5 6; 7 8 9]+72;
n = sqrt(size(x,3));
x2 = mat2cell(x, size(x,1), size(x,2), ones(1,size(x,3)));
x2 = reshape(x2,n,n)';
x2 = cell2mat(x2);
x2 =
1 2 3 10 11 12 19 20 21
4 5 6 13 14 15 22 23 24
7 8 9 16 17 18 25 26 27
28 29 30 37 38 39 46 47 48
31 32 33 40 41 42 49 50 51
34 35 36 43 44 45 52 53 54
55 56 57 64 65 66 73 74 75
58 59 60 67 68 69 76 77 78
61 62 63 70 71 72 79 80 81

댓글 수: 7

konstantina vatavali
konstantina vatavali 2020년 4월 11일
thank you very much for your reply!
this gives me error that the number of elements in reshape must not change
What is the size of matrix 'x'? Please paste the output of
size(x)
konstantina vatavali
konstantina vatavali 2020년 4월 11일
size x is 9x9x625
size x2 must be 225*225
konstantina vatavali
konstantina vatavali 2020년 4월 11일
ok I found it
It works now thank you!
Ameer Hamza
Ameer Hamza 2020년 4월 11일
I created random matrix of this size and the code work fine for me. Can you make sure that you correctly copied the code?
konstantina vatavali
konstantina vatavali 2020년 4월 12일
yes it works i made a mistake thank you!
Ameer Hamza
Ameer Hamza 2020년 4월 12일
Glad to be of help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

질문:

2020년 4월 11일

댓글:

2020년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by