Thread Subject: Get the first element out of each cell of a cell array

Subject: Get the first element out of each cell of a cell array

From: David Doria

Date: 11 Mar, 2008 20:56:02

Message: 1 of 4

I have a cell 4x20 cell array called L

each element of L is a 1x20 vector

How do I make a vector of all the first elements of L(1,:)?

I tried L(1,:)(1) to no avail.

Please let me know,

Thanks!

David

Subject: Get the first element out of each cell of a cell array

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Mar, 2008 21:24:35

Message: 2 of 4

In article <fr6rl1$ifj$1@fred.mathworks.com>,
David Doria <daviddoria@gmail.com> wrote:
>I have a cell 4x20 cell array called L

>each element of L is a 1x20 vector

>How do I make a vector of all the first elements of L(1,:)?
>
>I tried L(1,:)(1) to no avail.

cellfun(@(v) v(1), L(1,:))
--
  "MAMA: Oh--So now it's life. Money is life. Once upon a time freedom
   used to be life--now it's money. I guess the world really do change.
   WALTER: No--it was always money, Mama. We just didn't know about it."
                                              -- Lorraine Hansberry

Subject: Get the first element out of each cell of a cell array

From: us

Date: 12 Mar, 2008 05:34:01

Message: 3 of 4

"David Doria":
<SNIP first in line only...

> I have a cell 4x20 cell array called L
> each element of L is a 1x20 vector
> How do I make a vector of all the first elements of L
(1,:)?
> I tried L(1,:)(1) to no avail...

one of the many solutions

% the data
     c=cell(2,3); % <- your 4x20 cell array
     [c{:}]=deal(1:3); % <- each element being a vec
% the engine
     ca=cat(1,c{:});
     r=ca(:,1);
% the result (as expected)
     disp(c);
     disp(r.');
% 1 1 1 1 1 1

us
     

Subject: Get the first element out of each cell of a cell array

From: Loren Shure

Date: 13 Mar, 2008 12:41:58

Message: 4 of 4

In article <fr7q09$1vn$1@fred.mathworks.com>, us@neurol.unizh.ch says...
> "David Doria":
> <SNIP first in line only...
>
> > I have a cell 4x20 cell array called L
> > each element of L is a 1x20 vector
> > How do I make a vector of all the first elements of L
> (1,:)?
> > I tried L(1,:)(1) to no avail...
>
> one of the many solutions
>
> % the data
> c=cell(2,3); % <- your 4x20 cell array
> [c{:}]=deal(1:3); % <- each element being a vec
> % the engine
> ca=cat(1,c{:});
> r=ca(:,1);
> % the result (as expected)
> disp(c);
> disp(r.');
> % 1 1 1 1 1 1
>
> us
>
>
caveat that all cells need to have same number of elements for this
solution, I think.

the cellfun solution listed earlier works even if that's not the case.

--
Loren
http://blogs.mathworks.com/loren/

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
code us 12 Mar, 2008 01:35:12
deal us 12 Mar, 2008 01:35:12
cat us 12 Mar, 2008 01:35:12
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com