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:
cell array

Subject: cell array

From: yannan wu

Date: 30 Nov, 2011 22:55:08

Message: 1 of 10

I have a cell array:cell={[100,30,79,88,28,43,1] [100,91,33,36,54,8,5,2] [100,6,3] [100,91,33,36,54,8,5,2,4] [100,91,33,36,54,8,5] [100,6] [100,9,42,61,11,7] [100,91,33,36,54,8] [100,9] [100,6,3,23,10] [100,9,42,61,11] [100,91,33,36,31,99,12]}

I would like to obtain a cell array containing the second number of each element.for example cell2={[30] [91] [6] [91]....}.

How can I do that Thanks.

Subject: cell array

From: ade77

Date: 30 Nov, 2011 23:34:19

Message: 2 of 10

cell2 = cellfun(@(x) x(2), cell, 'un', 0) ......this will give u ur answer in cell array.

if u want it in just an array:
cell2 = cellfun(@(x) x(2), cell)

Subject: cell array

From: yannan wu

Date: 1 Dec, 2011 05:42:08

Message: 3 of 10

"ade77 " <ade100a@gmail.com> wrote in message <jb6ehr$d00$1@newscl01ah.mathworks.com>...
> cell2 = cellfun(@(x) x(2), cell, 'un', 0) ......this will give u ur answer in cell array.
>
> if u want it in just an array:
> cell2 = cellfun(@(x) x(2), cell)

Actually, I forgot the most important thing that this cell array contains an element that is only one number, for example cell={[100] [100,30,79,88,28,43,1] [100,91,33,36,54,8,5,2] [100,6,3] [100,91,33,36,54,8,5,2,4] [100,91,33,36,54,8,5] [100,6] [100,9,42,61,11,7] [100,91,33,36,54,8] [100,9] [100,6,3,23,10] [100,9,42,61,11] [100,91,33,36,31,99,12]}

I need to display the second number of each element, if the element doesn't have the second number, it would be 0 or empty instead . for example cell2={[0]{[30] [91] [6] [91]....}.

Subject: cell array

From: ade77

Date: 1 Dec, 2011 16:09:08

Message: 4 of 10

"Matlab pursing" wrote in message <jb743g$f78$1@newscl01ah.mathworks.com>...
> "ade77 " <ade100a@gmail.com> wrote in message <jb6ehr$d00$1@newscl01ah.mathworks.com>...
> > cell2 = cellfun(@(x) x(2), cell, 'un', 0) ......this will give u ur answer in cell array.
> >
> > if u want it in just an array:
> > cell2 = cellfun(@(x) x(2), cell)
>
> Actually, I forgot the most important thing that this cell array contains an element that is only one number, for example cell={[100] [100,30,79,88,28,43,1] [100,91,33,36,54,8,5,2] [100,6,3] [100,91,33,36,54,8,5,2,4] [100,91,33,36,54,8,5] [100,6] [100,9,42,61,11,7] [100,91,33,36,54,8] [100,9] [100,6,3,23,10] [100,9,42,61,11] [100,91,33,36,31,99,12]}
>
> I need to display the second number of each element, if the element doesn't have the second number, it would be 0 or empty instead . for example cell2={[0]{[30] [91] [6] [91]....}.



Well for now , u can use a for loop, someone might be able to give u a one liner using cellfun later.

c = {[100] [100,30,79,88,28,43,1] [100,91,33,36,54,8,5,2] [100,6,3] [100,91,33,36,54,8,5,2,4] [100,91,33,36,54,8,5] [100,6] [100,9,42,61,11,7] [100,91,33,36,54,8] [100,9] [100,6,3,23,10] [100,9,42,61,11] [100,91,33,36,31,99,12]};

cell2 = cell(1, size(c, 2)); %preallocate


for ii = 1: size(c,2)
if isscalar(c{ii})
    cell2{ii} = 0;
else
    cell2{ii} = c{ii}(2);
end
end

Subject: cell array

From: ade77

Date: 1 Dec, 2011 16:56:08

Message: 5 of 10

ce = { [100] [100,30,79,88,28,43,1] [100,91,33,36,54,8,5,2] [100,6,3] [100,91,33,36,54,8,5,2,4] [100,91,33,36,54,8,5] [100,6] [100,9,42,61,11,7] [100,91,33,36,54,8] [100,9] [100,6,3,23,10] [100,9,42,61,11] [100,91,33,36,31,99,12]};


This is a dubious one liner:
 cellfun(@(y) y(2), cellfun(@(x) [x 0], ce, 'Un', 0), 'Un', 0)

if u want it in vectors:
cellfun(@(y) y(2), cellfun(@(x) [x 0], ce, 'Un', 0))

Subject: cell array

From: Matlab pursing

Date: 1 Dec, 2011 18:55:10

Message: 6 of 10

"ade77 " <ade100a@gmail.com> wrote in message <jb8bj8$i71$1@newscl01ah.mathworks.com>...
> ce = { [100] [100,30,79,88,28,43,1] [100,91,33,36,54,8,5,2] [100,6,3] [100,91,33,36,54,8,5,2,4] [100,91,33,36,54,8,5] [100,6] [100,9,42,61,11,7] [100,91,33,36,54,8] [100,9] [100,6,3,23,10] [100,9,42,61,11] [100,91,33,36,31,99,12]};
>
>
> This is a dubious one liner:
> cellfun(@(y) y(2), cellfun(@(x) [x 0], ce, 'Un', 0), 'Un', 0)
>
> if u want it in vectors:
> cellfun(@(y) y(2), cellfun(@(x) [x 0], ce, 'Un', 0))

Thanks for your help. It works perfectly.

Subject: cell array

From: james bejon

Date: 2 Dec, 2011 21:23:08

Message: 7 of 10

No better really, but you can use

cellfun(@(x) x(2:numel(x)), ce, 'UniformOutput', 0)

if you want the cell to be empty when its array consists of only one element

Subject: cell array

From: ade77

Date: 2 Dec, 2011 21:38:08

Message: 8 of 10

"james bejon" wrote in message <jbbfjr$8m9$1@newscl01ah.mathworks.com>...
> No better really, but you can use
>
> cellfun(@(x) x(2:numel(x)), ce, 'UniformOutput', 0)
>
> if you want the cell to be empty when its array consists of only one element

the OP wants the second element from each cell, so this will not work.

Subject: cell array

From: james bejon

Date: 2 Dec, 2011 21:54:08

Message: 9 of 10

Oh, I see. Sorry, misread the OP. In which case:

cellfun(@(x) x(2:min(2,numel(x))), ce, 'UniformOutput', 0)

Subject: cell array

From: ade77

Date: 2 Dec, 2011 22:16:08

Message: 10 of 10

"james bejon" wrote in message <jbbhe0$eet$1@newscl01ah.mathworks.com>...
> Oh, I see. Sorry, misread the OP. In which case:
>
> cellfun(@(x) x(2:min(2,numel(x))), ce, 'UniformOutput', 0)

beautiful.

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