5

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

Eg:

matrixSize= 50*123456
N=50
L=100

Output will be 1*1235 cell and each cell has a 50*L matrix (last cell has only 50*56)

I know there is a function mat2cell in matlab:

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

But it doesn't sound an intuitive solution.

So is there a good solution?

1
  • Your example matrix sizes are inconsistent. I don't see how the output can be achieved that way with your example parameters. Also, what exactly is a "cell trunk"? Commented Aug 12, 2014 at 4:07

2 Answers 2

4

If I understand you correctly, assuming your matrix is denoted 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 a single line if you wish)... Lets test with some minimal numbers:

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

yields:

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

note that I didn't check for the exact dimension in the reshape, so you may need to reshape to ...,[],N*L) etc.

Sign up to request clarification or add additional context in comments.

2 Comments

for the sake of completeness this is the one line solution: a=[cellfun(@(n) reshape(n,N,L), num2cell(reshape(m(:,1:size(m,2)-mod(size(m,2),L)),N*L,[]),1),'un',0) ,{m(:,end-mod(size(m,2),L)+1:end)}];
+1 - You took a rather incomprehensible question and clarified with your answer. Very cool!
2

Just use elementary maths.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.