Thread Subject: concatenate cell array

Subject: concatenate cell array

From: Diego Lass

Date: 1 Jul, 2009 18:08:01

Message: 1 of 6

Hi
I would like to concatenate cell array in a loop

A = {}
for ...
     if ...
         concatenate A
      end
end

What is the most efficient way to do this?

p.s. I tried A = { A, {new cell array} }, it did not work.

Thanks
Diego

Subject: concatenate cell array

From: us

Date: 1 Jul, 2009 18:30:17

Message: 2 of 6

"Diego Lass" <dlISCool@gmail.com> wrote in message <h2g8m1$e5k$1@fred.mathworks.com>...
> Hi
> I would like to concatenate cell array in a loop
>
> A = {}
> for ...
> if ...
> concatenate A
> end
> end
>
> What is the most efficient way to do this?
>
> p.s. I tried A = { A, {new cell array} }, it did not work.
>
> Thanks
> Diego

one of the solutions
- iff you know the loop's max...

     n=5;
     c=cell(n,1); % <- preallocation
for i=1:n
     c{i}=rand(1,i); % <- index into preallocated CELL
end

us

Subject: concatenate cell array

From: Diego Lass

Date: 1 Jul, 2009 18:44:02

Message: 3 of 6

Unfortunately, I don't know the size before hand.
for ..
    if GOOD
        concatenate A
   end
 end

If determines whether I should concatenate A

Diego


"us " <us@neurol.unizh.ch> wrote in message <h2g9vp$7rm$1@fred.mathworks.com>...
> "Diego Lass" <dlISCool@gmail.com> wrote in message <h2g8m1$e5k$1@fred.mathworks.com>...
> > Hi
> > I would like to concatenate cell array in a loop
> >
> > A = {}
> > for ...
> > if ...
> > concatenate A
> > end
> > end
> >
> > What is the most efficient way to do this?
> >
> > p.s. I tried A = { A, {new cell array} }, it did not work.
> >
> > Thanks
> > Diego
>
> one of the solutions
> - iff you know the loop's max...
>
> n=5;
> c=cell(n,1); % <- preallocation
> for i=1:n
> c{i}=rand(1,i); % <- index into preallocated CELL
> end
>
> us

Subject: concatenate cell array

From: Ravi

Date: 1 Jul, 2009 18:46:01

Message: 4 of 6

this shud work..

a=[1 2; 3 4];
b={};
b=cat(1,b,a) %this will concatenat i dim 1


"Diego Lass" <dlISCool@gmail.com> wrote in message <h2g8m1$e5k$1@fred.mathworks.com>...
> Hi
> I would like to concatenate cell array in a loop
>
> A = {}
> for ...
> if ...
> concatenate A
> end
> end
>
> What is the most efficient way to do this?
>
> p.s. I tried A = { A, {new cell array} }, it did not work.
>
> Thanks
> Diego

Subject: concatenate cell array

From: Diego Lass

Date: 1 Jul, 2009 18:59:02

Message: 5 of 6

I figured out!

goodCounter = 0;

For...
    if good
       goodCounter = goodCounter + 1;
       A{goodCounter} = {new cell};
    end
end

Diego

"Ravi " <ravi.chilumula@gmail.com> wrote in message <h2gat9$77g$1@fred.mathworks.com>...
> this shud work..
>
> a=[1 2; 3 4];
> b={};
> b=cat(1,b,a) %this will concatenat i dim 1
>
>
> "Diego Lass" <dlISCool@gmail.com> wrote in message <h2g8m1$e5k$1@fred.mathworks.com>...
> > Hi
> > I would like to concatenate cell array in a loop
> >
> > A = {}
> > for ...
> > if ...
> > concatenate A
> > end
> > end
> >
> > What is the most efficient way to do this?
> >
> > p.s. I tried A = { A, {new cell array} }, it did not work.
> >
> > Thanks
> > Diego

Subject: concatenate cell array

From: us

Date: 1 Jul, 2009 19:03:01

Message: 6 of 6

"Diego Lass" <dlISCool@gmail.com> wrote in message <h2gaph$sv6$1@fred.mathworks.com>...
> Unfortunately, I don't know the size before hand.
> for ..
> if GOOD
> concatenate A
> end
> end
>
> If determines whether I should concatenate A
>
> Diego

one of the solutions is outlined below

     c={};
for i=1:n
if bitand(i,1)
     c=[c,{i:i}];
end
end

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
syntax us 1 Jul, 2009 14:34:05
code us 1 Jul, 2009 14:34:05
cell us 1 Jul, 2009 14:34:05
cell array Diego Lass 1 Jul, 2009 14:09:08
rssFeed for this Thread

Contact us at files@mathworks.com