Convert a matrix to an array of cells of an array of cells

I want to change the matrix N * 123456 to cell cells, each sub-cell contains the matrix N * L

For instance:

matrixSize= 50*123456 N=50 L=100 

The output will be 1 * 1235, and each cell has a 50 * L matrix (the last cell has only 50 * 56)

I know that in matlab there is a mat2cell function:

 Output = mat2cell(x, [50], [100,100,100,......56]) 

But this does not seem to be an intuitive solution.

So is there a good solution?

+5
source share
2 answers

If you understood correctly, assuming that your matrix is โ€‹โ€‹denoted by m , this is what you wanted:

 a=num2cell(reshape(m(:,1:size(m,2)-mod(size(m,2),L)),N*L,[]),1); a=cellfun(@(n) reshape(n,N,L), a,'UniformOutput',false); a{end+1}=m(:,end-mod(size(m,2),L)+1:end); 

(this can be shortened to one line if you want) ... Let's check a few minimum numbers:

 m=rand(50,334); N=50; L=100; 

gives:

 a = [50x100 double] [50x100 double] [50x100 double] [50x34 double] 

Note that I did not check the exact size in the change, so you may need to change the shape to ...,[],N*L) , etc.

+4
source

Just use elementary maths.

 q = floor(123456/100); r = rem(123456,100); Output = mat2cell(x, 50, [repmat(100,1,q),r]) 
+2
source

Source: https://habr.com/ru/post/1200077/


All Articles