How to reshape matlab matrices for this example?

淺唱寂寞╮ 提交于 2019-12-12 21:28:51

问题


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.


回答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));



回答2:


Consider using the reshape function: doc@mathworks.




回答3:


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{:})



回答4:


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



来源:https://stackoverflow.com/questions/11046415/how-to-reshape-matlab-matrices-for-this-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!