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:
select the first element of each cell

Subject: select the first element of each cell

From: Wendy

Date: 2 Aug, 2011 13:05:13

Message: 1 of 5

Hi all,

I have a 211*1 cell array, and each cell has to elements. I want to make a new variable that is composed of the first element from each cell. Is there a function for doing this more efficiently than a for loop?

Thank you very much.
Wendy

Subject: select the first element of each cell

From: Matt J

Date: 2 Aug, 2011 14:11:09

Message: 2 of 5

"Wendy " <wlq121@gmail.com> wrote in message <j18sm9$gdc$1@newscl01ah.mathworks.com>...
> Hi all,
>
> I have a 211*1 cell array, and each cell has to elements. I want to make a new variable that is composed of the first element from each cell. Is there a function for doing this more efficiently than a for loop?
===============

No, but you can abbreviate syntax this way

myCell={[10,2], [3,4]};
result = cellfun(@(c) c(1), myCell)

Subject: select the first element of each cell

From: dpb

Date: 2 Aug, 2011 14:13:40

Message: 3 of 5

On 8/2/2011 8:05 AM, Wendy wrote:
> Hi all,
> I have a 211*1 cell array, and each cell has to elements. I want to make
> a new variable that is composed of the first element from each cell. Is
> there a function for doing this more efficiently than a for loop?

Unfortunately, afaik, no...

ML needs some syntax to dereference cell contents in nested fashion but
hasn't anything (and I'm not sure how one could write it that it
wouldn't be unambiguous, but it would be a very useful feature if could
be managed).

--

Subject: select the first element of each cell

From: Leo Kay

Date: 30 Oct, 2011 13:16:09

Message: 4 of 5

dpb <none@non.net> wrote in message <j190mg$8lm$1@speranza.aioe.org>...
> On 8/2/2011 8:05 AM, Wendy wrote:
> > Hi all,
> > I have a 211*1 cell array, and each cell has to elements. I want to make
> > a new variable that is composed of the first element from each cell. Is
> > there a function for doing this more efficiently than a for loop?
>
> Unfortunately, afaik, no...
>
> ML needs some syntax to dereference cell contents in nested fashion but
> hasn't anything (and I'm not sure how one could write it that it
> wouldn't be unambiguous, but it would be a very useful feature if could
> be managed).
>
> --


I have thought carefully about his problem, and the best to resolve it would be to convert the cell to another object, where you can index each element of said new object. Alternatively, he can create a new cell using the proposed method above, but index the new cell such that each row contains exactly one element from the old cell. That should be possible with cell fun and using preallocation.

Subject: select the first element of each cell

From: Leo Kay

Date: 30 Oct, 2011 13:17:08

Message: 5 of 5

dpb <none@non.net> wrote in message <j190mg$8lm$1@speranza.aioe.org>...
> On 8/2/2011 8:05 AM, Wendy wrote:
> > Hi all,
> > I have a 211*1 cell array, and each cell has to elements. I want to make
> > a new variable that is composed of the first element from each cell. Is
> > there a function for doing this more efficiently than a for loop?
>
> Unfortunately, afaik, no...
>
> ML needs some syntax to dereference cell contents in nested fashion but
> hasn't anything (and I'm not sure how one could write it that it
> wouldn't be unambiguous, but it would be a very useful feature if could
> be managed).
>
> --


I have thought carefully about his problem, and the best to resolve it would be to convert the cell to another object, where you can index each element of said new object. Alternatively, he can create a new cell using the proposed method above, but index the new cell such that each row contains exactly one element from the old cell. That should be possible with cell fun and using preallocation.

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