For Programmers: Free Programming Magazines  


Home > Archive > Matlab > March 2006 > cell array (diff size) to matrix









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cell array (diff size) to matrix
sunny

2006-03-31, 10:03 pm

Hi,

I want to convert cell array A to a matrix B.

A=

Columns 1 through 6

[0] [7x1 double] [8x1 double] [4x1 double] [2x1 double]
[0]

Columns 7 through 11

[2x1 double] [0] [0] [2x1 double] [0]


A is a cell array of column vectors.
B will be of size (max row value of column vector) x (num of column
vector)
For the above example B will be 8 x 11 ...

Can I do this without using a loop ?

Thanks.
Santosh.

Jos

2006-03-31, 10:03 pm

sunny wrote:
>
>
> Hi,
>
> I want to convert cell array A to a matrix B.
>
> A=
>
> Columns 1 through 6
>
> [0] [7x1 double] [8x1 double] [4x1 double] [2x1
> double]
> [0]
>
> Columns 7 through 11
>
> [2x1 double] [0] [0] [2x1 double] [0]
>
>
> A is a cell array of column vectors.
> B will be of size (max row value of column vector) x (num of column
> vector)
> For the above example B will be 8 x 11 ...
>
> Can I do this without using a loop ?
>
> Thanks.
> Santosh.
>
>


You have to fill in the unoccupied parts of B with, e.g., NaNs. A
solution:

% the data
A = {[0] , [2;3;4],[0],[5;6],[],[2;4;6;8]} ;
% the engine
n = cellfun('length',A) ;
x = cumsum([n(:).' ; repmat(-1,max(n)-1,length(n))])>0 ;
cx = cumsum(double(x(:))) ;
C = cat(1,A{:}) ;
B = repmat(NaN,size(x)) ;
B(x) = C(cx(x)) ;
% the result
B

hth
Jos
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com