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:
Create duplicate values in cell array

Subject: Create duplicate values in cell array

From: Ahmad

Date: 20 Apr, 2008 11:19:02

Message: 1 of 5

Hello there,

I have a 1 column cell array with different values and I
have a vector with the same number of rows. I need to expand
the cell array (as in create duplication of values)
according to the values in the vector. Don't worry I'll
illustrate this with an example:
c = { [2]; [4]; [6]; [8] }

and I have a vector
v = [ 1; 2; 1; 3 ]

I need to duplicate out the values in the cell array by the
number given in v at each row i.e. in the first row of c, 2
should occur just once since v has 1 in that column, but 4,
in the second row, should occur twice since there is 2 in
the associated row in the vector

In the end I need this result:
ans = { [2]; [4 4]; [6]; [8 8 8] }

Is there a one liner for this?


Thanks,

Subject: Create duplicate values in cell array

From: Bruno Luong

Date: 20 Apr, 2008 11:25:05

Message: 2 of 5

"Ahmad " <ahmad.humyn@gmail.com> wrote in message
<fuf8r6$s5o$1@fred.mathworks.com>...

>
> Is there a one liner for this?
>
>

This should be it:

d=arrayfun(@(val, n) repmat(val{1},1,n), c, v,...
           'Uniformoutput', false)

Bruno

Subject: Create duplicate values in cell array

From: Ahmad

Date: 20 Apr, 2008 11:54:03

Message: 3 of 5

Thanks again Bruno, you keep saving my day :)


Regards,

"Bruno Luong" <b.luong@fogale.fr> wrote in message
<fuf96h$pv3$1@fred.mathworks.com>...
> "Ahmad " <ahmad.humyn@gmail.com> wrote in message
> <fuf8r6$s5o$1@fred.mathworks.com>...
>
> >
> > Is there a one liner for this?
> >
> >
>
> This should be it:
>
> d=arrayfun(@(val, n) repmat(val{1},1,n), c, v,...
> 'Uniformoutput', false)
>
> Bruno

Subject: Create duplicate values in cell array

From: Emilie

Date: 16 Aug, 2013 20:02:07

Message: 4 of 5

Hi, I have a similar problem, but don't quite understand your solution, Bruno. Maybe you could help me with my problem?
I also want to duplicate values in a vector from numbers in another vector. I have two vectors, lets say
a = [1 4 0 2] and b = [21 56 32 90]
and I want one long column vector that displays the numbers in b as many times as the numbers in a indicate. So that I end up with a column vector;
c = [21 56 56 56 56 90 90]


"Bruno Luong" <b.luong@fogale.fr> wrote in message <fuf96h$pv3$1@fred.mathworks.com>...
> "Ahmad " <ahmad.humyn@gmail.com> wrote in message
> <fuf8r6$s5o$1@fred.mathworks.com>...
>
> >
> > Is there a one liner for this?
> >
> >
>
> This should be it:
>
> d=arrayfun(@(val, n) repmat(val{1},1,n), c, v,...
> 'Uniformoutput', false)
>
> Bruno

Subject: Create duplicate values in cell array

From: Bruno Luong

Date: 24 Aug, 2013 14:26:16

Message: 5 of 5

"Emilie " <emilieci@hotmail.com> wrote in message <kum0fv$pcf$1@newscl01ah.mathworks.com>...
> Hi, I have a similar problem, but don't quite understand your solution, Bruno. Maybe you could help me with my problem?
> I also want to duplicate values in a vector from numbers in another vector. I have two vectors, lets say
> a = [1 4 0 2] and b = [21 56 32 90]
> and I want one long column vector that displays the numbers in b as many times as the numbers in a indicate. So that I end up with a column vector;
> c = [21 56 56 56 56 90 90]

c = cell2mat(arrayfun(@(a,b) b+zeros(1,a), a, b, 'unif', 0))

% Bruno

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