Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Working with cell2mat

Subject: Working with cell2mat

From: Diego Zegarra

Date: 16 Feb, 2009 18:26:01

Message: 1 of 7

Hello everyone,

Quick question here, if we have

a{1,1} = [5 4 3 2]
a{2,1} = [5 4 3 2]

Now if I do cell2mat(a) I get the following matrix,

5 4 3 2
5 4 3 2

Now what if I had instead this,

a{1,1} = [5 4 3 2]
a{2,1} = [5 3 2]

Is there anyway that I can get the following matrix,

5 4 3 2
5 3 2 0

If they are not the same size just fill the rest with a zero so they can be same size. I know cell2mat only works if the dimensions are the same.

Thanks!

Subject: Working with cell2mat

From: Matt Fig

Date: 16 Feb, 2009 19:02:01

Message: 2 of 7

For a general case, like this:

a{1,1} = [5 4 3 2];
a{2,1} = [];
a{3,1} = [5 4 3 2 8];
a{4,1} = [5 3 ];
a{5,1} = [5];
a{6,1} = [3 4 5 6 7 8];
a{7,1} = [5 3 2];

A simple loop works:

lns = cellfun(@length,a);
mx = max(lns);
mat = 0;

for ii = 1:numel(a)
    a{ii,1} = [a{ii,1} mat(1,ones(1,mx-lns(ii)))];
end

cell2mat(a)


There might be a vectorized solution, maybe an accumarray solution, but this is easy enough with loops.




Z[2fclRlalUlNssN\RN:\N]Y'bTalZ-\NV\UNllzPXUNVRl]aO\FPbO[Y\R

Subject: Working with cell2mat

From: us

Date: 16 Feb, 2009 19:32:01

Message: 3 of 7

"Matt Fig"
> There might be a vectorized solution, maybe an accumarray solution, but this is easy enough with loops...

correct...
...just to show yet another solution

% the data
     m=cell(5,1);
     m{1}=[1,2,3];
     m{3}=[4,5];
     m{4}=[6,7,8,9];
% the engine
     nz=max(cellfun(@numel,m));
     r=cell2mat(cellfun(@(x) [x,zeros(1,nz-numel(x))],m,'uni',false));
% the result
     disp(r);
%{
     1 2 3 0
     0 0 0 0
     4 5 0 0
     6 7 8 9
     0 0 0 0
%}

us

Subject: Working with cell2mat

From: Matt Fig

Date: 16 Feb, 2009 19:39:01

Message: 4 of 7

I was thinking, since I used a loop, why bother with cell2mat? Here is another loop solution, though us showed a vectorized one (for very large cell arrays, the loop *might* be faster).


lns = cellfun(@length,a);
mt = zeros(numel(a),max(lns));

for ii = 1:numel(a)
    mt(ii,1:lns(ii)) = a{ii};
end
mt




^_X^NhUhRVRh]JJvWQhNhJYo]WJXU)XNQJhQ6]L#VoBLJXhKJNhPKTX.bXY

Subject: Working with cell2mat

From: us

Date: 16 Feb, 2009 20:05:04

Message: 5 of 7

"Matt Fig"
> Here is another loop solution, though us showed a vectorized one (for very large cell arrays, the loop *might* be faster)...

...as had to BE expected, the looper runs MUCH faster...

% the data
     nr=100; % #runs
     cs=1000; % #cell rows
     mc=10; % max #col/row
     m=cell(cs,1);
     m=cellfun(@(x) 1:ceil(mc*rand),m,'uni',false);
% the solutions
% 1) matt fig
     a=m;
     tic;
for i=1:nr
     lns=cellfun(@length,a);
     mt=zeros(numel(a),max(lns));
for ii = 1:numel(a)
     mt(ii,1:lns(ii)) = a{ii};
end
end
     t1=toc;
     r1=mt;
% 2) us
     tic;
for i=1:nr
     nz=max(cellfun(@numel,m));
     r2=cell2mat(cellfun(@(x) [x,zeros(1,nz-numel(x))],m,'uni',false));
end
     t2=toc;
% the result
     disp(sprintf('%10.7f\n',isequal(r1,r2),t1,t2,100*t1/t2));
%{
% wintel system
% ic2/2*2.6ghz/2gb/winxp.sp3/r2008b
     1.0000000 % res are equal
     0.2894584 % matt fig
     1.1760158 % us
    24.6134762 % gain
%}

us

Subject: Working with cell2mat

From: Matt Fig

Date: 16 Feb, 2009 20:12:02

Message: 6 of 7

But it is not as neat! Oh, the trade-offs in life! ;)




6elc]_]dujkk{]p*^jrhgI{]{kk#pi]ak#A^]a{{aldepqq{ih]d_U<{{ka

Subject: Working with cell2mat

From: Jos

Date: 17 Feb, 2009 07:37:04

Message: 7 of 7

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gncb3p$t0d$1@fred.mathworks.com>...
> Hello everyone,
>
> Quick question here, if we have
>
> a{1,1} = [5 4 3 2]
> a{2,1} = [5 4 3 2]
>
> Now if I do cell2mat(a) I get the following matrix,
>
> 5 4 3 2
> 5 4 3 2
>
> Now what if I had instead this,
>
> a{1,1} = [5 4 3 2]
> a{2,1} = [5 3 2]
>
> Is there anyway that I can get the following matrix,
>
> 5 4 3 2
> 5 3 2 0
>
> If they are not the same size just fill the rest with a zero so they can be same size. I know cell2mat only works if the dimensions are the same.
>
> Thanks!

a = {[5 4 3 2], [5 3 2]} ;
[b,tf] = padcat(a{:})
b(~tf) = 0

PADCAT can be found on the FEX:
http://www.mathworks.com/matlabcentral/fileexchange/22909

hth
Jos

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us