Thread Subject: Vectorization with cell array

Subject: Vectorization with cell array

From: Andrew

Date: 23 Jan, 2008 09:18:01

Message: 1 of 6

I have a problem whereby I wish to vectorize the assignment
to a cell array. If I have
ndx=1:2;
B(ndx)={'First', 'Second'};
C(ndx)=[1 2];
I can then create a structure with a cell array by
Test.test(ndx(1),:)={B(ndx(1)), C(ndx(1))};
and do the same for ndx(2), but how to do the following:
Test.test(ndx,:)={B(ndx), C(ndx)};
hence getting rid of the for loop. I have been looking at
functions like cellfun and arrayfun, but no luck yet.

Thanks in advance for the help.
Andrew

Subject: Vectorization with cell array

From: us

Date: 23 Jan, 2008 10:04:02

Message: 2 of 6

"Andrew ":
<SNIP cell acrobatics...

one of the solutions

% the data
     b={'first','second'};
     c=[1,2];
% the engine
     clear s v; % save old stuff!
     v=cellfun(@(a,b) {a,b},b,num2cell(c),'uni',false);
     s=struct('f',v);
% the result
     s.f
% ans = 'first' [1]
% ans = 'second' [2]

us

Subject: Vectorization with cell array

From: Yumnam Kirani Singh

Date: 23 Jan, 2008 10:57:52

Message: 3 of 6

Have you tried this way.
Test.Test(ndx)={B;C}

Subject: Vectorization with cell array

From: Andrew

Date: 23 Jan, 2008 15:55:03

Message: 4 of 6

"us " <us@neurol.unizh.ch> wrote in message
<fn73ei$afj$1@fred.mathworks.com>...
> "Andrew ":
> <SNIP cell acrobatics...
>
> one of the solutions
>
> % the data
> b={'first','second'};
> c=[1,2];
> % the engine
> clear s v; % save old stuff!
> v=cellfun(@(a,b) {a,b},b,num2cell(c),'uni',false);
> s=struct('f',v);
> % the result
> s.f
> % ans = 'first' [1]
> % ans = 'second' [2]
>
> us
Thanks but these solutions do not quite achieve what I am
looking for. In the example above v is a cell array of cell
arrays. I am looking to produce a single cell array where
v(1,1) is 'first', v(1,2)=1, v(2,1)='second' and so on. I
guess I could do [v{:}] and then reshape the output, but a
neater solution would be preferable.

Andrew

Subject: Vectorization with cell array

From: us

Date: 23 Jan, 2008 17:29:02

Message: 5 of 6

"Andrew ":
<SNIP changing the problem...

> Thanks but these solutions do not quite achieve what I am
> looking for.
> In the example above v is a cell array of cell
> arrays. I am looking to produce a single cell array where
> v(1,1) is 'first', v(1,2)=1, v(2,1)='second' and so on...

well, what you want NOW is NOT what you wanted before...
running your initial example yields this

     Test.test(1,1) % <- array index
% {1x1 cell}
     Test.test{1,1} % <- cell contents
% 'First' % -> now your v(1,1)

your <acutal> example could be achieved by this

     clear s;
     b={'first','second'};
     c=[1,2];
     d=num2cell(c);
     s.f=[b.',d.'];
% some results
     s.f
     s.f(1,1)
     s.f(2,2)

us

Subject: Vectorization with cell array

From: Andrew

Date: 24 Jan, 2008 09:36:02

Message: 6 of 6

"us " <us@neurol.unizh.ch> wrote in message
<fn7tgu$g8r$1@fred.mathworks.com>...
> "Andrew ":
> <SNIP changing the problem...
>
> > Thanks but these solutions do not quite achieve what I am
> > looking for.
> > In the example above v is a cell array of cell
> > arrays. I am looking to produce a single cell array where
> > v(1,1) is 'first', v(1,2)=1, v(2,1)='second' and so on...
>
> well, what you want NOW is NOT what you wanted before...
> running your initial example yields this
>
> Test.test(1,1) % <- array index
> % {1x1 cell}
> Test.test{1,1} % <- cell contents
> % 'First' % -> now your v(1,1)
>
> your <acutal> example could be achieved by this
>
> clear s;
> b={'first','second'};
> c=[1,2];
> d=num2cell(c);
> s.f=[b.',d.'];
> % some results
> s.f
> s.f(1,1)
> s.f(2,2)
>
> us
Thanks for the reply again. This is what I wanted to do and
if you do run the example I gave in my first post using
ndx(1) and ndx(2) you will end up with the same cell array
as you acheived by vectorization.

I appreciate the help! Andrew

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
struct us 23 Jan, 2008 05:05:06
num2cell us 23 Jan, 2008 05:05:06
code us 23 Jan, 2008 05:05:06
cellfun us 23 Jan, 2008 05:05:06
cell arrays Andrew 23 Jan, 2008 04:20:11
vectorization Andrew 23 Jan, 2008 04:20:11
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