Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
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].
Solved! Go to Solution.
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.
Mike
Luc Meekes wrote:
Here's a routine for variable k.
Success!
Luc
Success indeed.
Nice neat function.
Mike
Here's a function to do any number of repeats (and it has no dependency on other user defined functions)
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?
Your function is below which is slightly different from what you just posted,
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
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
Thank you Gentlemen, I have been pondering over this for three weeks.