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.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 11 Apr 2020
Edited: Ameer Hamza on 11 Apr 2020
% 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 Comments

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)
size x is 9x9x625
size x2 must be 225*225
ok I found it
It works now thank you!
I created random matrix of this size and the code work fine for me. Can you make sure that you correctly copied the code?
yes it works i made a mistake thank you!
Glad to be of help.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!