2

I have a 40x16 matrix or 8 5x16 one below the other i.e. aligned vertically. I want to get a 5x128 matrix from that such that I align the 8 5x16 matrices horizontally. is there an efficient/quicker (rather than the hardcoded for loops) way to do this?

I want the individual 5x16 matrices intact.

H.Muster
  • 9,297
  • 1
  • 35
  • 46
  • related questions: [split long 2D matrix into the third dimension](http://stackoverflow.com/q/1390909/97160), [MATLAB - re-arrange matrix by vertically concatenating submatrices](http://stackoverflow.com/q/10904579/97160) – Amro Jun 17 '12 at 16:57

4 Answers4

1

This should work. Suppose your matrix is A (40x16).

Here's a way using reshape:

m = 5; n = 8; p = 16;
B = reshape(permute(reshape(A', p, m, n), [2 1 3]), m, n*p);

B will have your eight 5x16 matrices next to each other, intact.

And here's a way without reshape:

m = 5; n = 8;
B = cell2mat(arrayfun(@(i) A(m*(i-1)+1:m*i, :), 1:n, 'UniformOutput', false));
Ansari
  • 8,168
  • 2
  • 23
  • 34
0

Consider using the reshape function: doc@mathworks.

wap26
  • 2,180
  • 1
  • 17
  • 32
0

You can use MAT2CELL to divide the big matrix into smaller ones, then combine along the dimension you want:

A = rand(8*5,16);
blkSz = 5;

C = mat2cell(A, blkSz*ones(1,size(A,1)/blkSz), size(A,2));
C = cat(2,C{:})
Amro
  • 123,847
  • 25
  • 243
  • 454
0

Reshape a 3-by-4 matrix into a 2-by-6 matrix. A = 1 4 7 10 2 5 8 11 3 6 9 12

B = reshape(A,2,6)

B = 1 3 5 7 9 11 2 4 6 8 10 12 B = reshape(A,2,[])

B = 1 3 5 7 9 11 2 4 6 8 10 12