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:
Duplicate vector values from numbers in another vector

Subject: Duplicate vector values from numbers in another vector

From: Emilie

Date: 16 Aug, 2013 20:12:07

Message: 1 of 5

Hi, I really hope someone can help me with this!!
I want to duplicate values in a vector the amount of times determined by values in another vector. Lets say I have these two simple vectors:
a = [1 4 0 2] and b = [21 56 32 90]
and I want one 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]
As the real vectors I'm working with are very long, with large numbers, I've tried to make a for loop by using the function 'repmat', like this :
for k=1:length(b)
      d = repmat(b(k),a(k),1);
end
but obviously the vector d keeps overwriting itself so that it only displays d=[90 90].
Any ideas on how I can get the result in c as easy as possible??

Subject: Duplicate vector values from numbers in another vector

From: dpb

Date: 16 Aug, 2013 21:22:37

Message: 2 of 5

On 8/16/2013 3:12 PM, Emilie wrote:
> Hi, I really hope someone can help me with this!!
> I want to duplicate values in a vector the amount of times determined by
> values in another vector. Lets say I have these two simple vectors:
> a = [1 4 0 2] and b = [21 56 32 90]
> and I want one 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]
...

 >> a = [1 4 0 2]; b = [21 56 32 90];
 >> cell2mat(arrayfun(@(x,nx) repmat(x,1,nx), b, a,'uniformoutput',0))
ans =
     21 56 56 56 56 90 90
 >>

Subject: Duplicate vector values from numbers in another vector

From: Roger Stafford

Date: 17 Aug, 2013 01:13:28

Message: 3 of 5

"Emilie " <emilieci@hotmail.com> wrote in message <kum12n$2ov$1@newscl01ah.mathworks.com>...
> .... Lets say I have these two simple vectors:
> a = [1 4 0 2] and b = [21 56 32 90]
> and I want one 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]
- - - - - - - - -
  Another possibility:

 t = accumarray(cumsum([1,a]),1);
 c = b(cumsum(t(1:end-1)));

Roger Stafford

Subject: Duplicate vector values from numbers in another vector

From: james bejon

Date: 20 Aug, 2013 14:20:21

Message: 4 of 5

Very nice. (You mean to transpose "cumsum([1,a])", right?)

Subject: Duplicate vector values from numbers in another vector

From: Jon

Date: 20 Aug, 2013 16:23:09

Message: 5 of 5

If you want to keep your for loop, you can do it like this:

d=[];
for k=1:length(b)
      d =[d; repmat(b(k),a(k),1)];
end

This appends the next set of numbers to the current vector instead of overwriting it.

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