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
> 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
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.
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.