Broadcast an Array N times
I feel like I have been coding for too long this afternoon, because for the life of me, I can't find a solution to what seems to be a simple problem.
I have an array that I need to extend multiple times over.
Dim dz() as Double
Redim dz(1 to 10)
I have the variable dz() full of 10 random numbers. I need to copy that number sequence (1 through 10) 10 times into another array.
Python does this quite easily, but I'm struggling to figure it out in VBA. Any thoughts on how you can do this?
Re: Broadcast an Array N times
Is the other array a 1d array with 100 elements? Or a 2d 10x10 array?
Code:
'1d pcode
for i = 1 to 100
otherArray(i) = dz(i mod 10)
next i
Code:
'2d pcode
for i = 1 to 10
for j = 1 to 10
otherArray(i, j) = dz(j)
next j
next i
Re: Broadcast an Array N times
Of course, that code is assuming that the destination array is pre-dimensioned to the size (or more) that you need.
Re: Broadcast an Array N times
OptionBase1, thank you for the suggestion. I did not think of doing that. It works great. I had to add some extra code to actually get the 10th value in dz(), because (i mod 10) evaluates to 0 when i is divisible by 10. Easy enough though with your help getting me started. Thanks again!.
And yea the destination array is pre-dimensioned passel.
Re: Broadcast an Array N times
Quote:
Originally Posted by
JohnnyWaffles
.... I had to add some extra code to actually get the 10th value in dz(), because (i mod 10) evaluates to 0 when i is divisible by 10. ...
That is why it is usually more useful to use arrays that start at 0 rather than 1, if you need to use Mod and other operations that will return 0.
But, to turn a 0 based index into 1 based index should have been a simple matter of adding 1 to the Mod result, so not need much extra code.
Code:
otherArray(i) = dz((i mod 10) + 1)