cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

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

truck_track
5-Regular Member

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

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].

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:truck_track)

Here's a routine for variable k.

Success!

Luc

View solution in original post

12 REPLIES 12
MikeArmstrong
5-Regular Member
(To:truck_track)

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

Updated version attached.

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

Mike

Improved function attached.

Removes the need of addition functions.

Improved Duplicate Function.jpg

Mike

LucMeekes
23-Emerald III
(To:truck_track)

Here's a routine for variable k.

Success!

Luc

MikeArmstrong
5-Regular Member
(To:LucMeekes)

Luc Meekes wrote:

Here's a routine for variable k.

Success!

Luc

Success indeed.

Nice neat function.

Mike

RichardJ
19-Tanzanite
(To:truck_track)

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

MikeArmstrong
5-Regular Member
(To:RichardJ)

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

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?

MikeArmstrong
5-Regular Member
(To:RichardJ)

Your function is below which is slightly different from what you just posted,

Clipboard01.jpg

I think perhaps I misunderstand your question?

Or perhaps my questions wasn't clear enough.

I know how you physically typed it, but cannot get my head around the way it works. When I said I have been writing it out long hand I meant for example.

First Loop

i = 0 n = 2 j = 0

This equates to zero, which is the first element in NewV - I think my understanding is correct this far, but once I get to the second loop I'm getting a little confused.

Mike

Take out the inner loop. Then NewV is a vector n times as long as V. Every nth element of NewV will be a non-zero number, because only the n*i th elements are assigned to anything. The inner loop is there to populate all the elements from n+i to n*i+(n-1) with V[i

MikeArmstrong
5-Regular Member
(To:RichardJ)

Got it, cheers Richard.

Because the inner loop is nested it runs through its complete set of values for each value of the outer for loop.

Learn something new every day hey!!!!

Mike

truck_track
5-Regular Member
(To:truck_track)

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

Top Tags