Got Questions? Get Answers.
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:
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

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