Skip to main content
6-Contributor
October 4, 2010
Solved

How do I repeat elements of a vector to create another larger vector?

  • October 4, 2010
  • 4 replies
  • 6117 views

If I have a vector V, how do I repeat values/elements into another vector to create a vector with more elements? For example; I have a vector V=[0,5,3,...n] and I want to repeat each element of this vector twice (k=2) to create a vector Z=[0,0,5,5,3,3,...n,n].

Best answer by LucMeekes

Here's a routine for variable k.

Success!

Luc

4 replies

1-Visitor
October 4, 2010

Something like the attached.

I have just knocked this up, but I'm sure I have seen a duplicate function in this collab before.

Mike

Message was edited by: MIke Armstrong

1-Visitor
October 4, 2010

Updated version attached.

Additional argument added for number of times each element in the vector is duplicated.

Mike

1-Visitor
October 5, 2010

Improved function attached.

Removes the need of addition functions.

Improved Duplicate Function.jpg

Mike

LucMeekes23-Emerald IVAnswer
23-Emerald IV
October 4, 2010

Here's a routine for variable k.

Success!

Luc

1-Visitor
October 4, 2010

Luc Meekes wrote:

Here's a routine for variable k.

Success!

Luc

Success indeed.

Nice neat function.

Mike

19-Tanzanite
October 4, 2010

Here's a function to do any number of repeats (and it has no dependency on other user defined functions)

1-Visitor
October 5, 2010

Would you be kind enough to explain how you have programmed the vector subscripts in your programme?

I have tried writing it out long hand without any success.

Cheers

Mike

19-Tanzanite
October 5, 2010
Would you be kind enough to explain how you have programmed the vector subscripts in your programme?

I have tried writing it out long hand without any success.

I just typed V[i*n+j

I think perhaps I misunderstand your question?

6-Contributor
October 4, 2010

Thank you Gentlemen, I have been pondering over this for three weeks.