LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Efficient way to replace array subset into an array at discontigous indices

Solved!
Go to solution

Hello Team,

 

There is an input array of size 4 which is fed as an input to a for loop. With in loop, i have an zero initialized array of size 100, in each iteration have to assign the input array at 4 different indices....so basically loop runs 25 times and in each iteration input array of size 4 gets assigned at 4 different indexes of 100 size array.

 

lets say iteration 1, indexes are 1,9,11,20 and data is D0,D1,D2,D3. So D0 is replaced at index1, D1 is replaced at index 9 and so on

 

Simplest way to implement is 4 array replacements per iteration....Is there any efficient way to implement it in a single replacement array
operation or min replacements per iteration. 1 efficient way is to create a mask and then use it to implement the particular case. 

 

Any advice or suggestion will be of great help.

 

Thanks

Siddharth

0 Kudos
Message 1 of 14
(1,747 Views)

Stop and think about it a moment.  If the 4 destinations can be any arbitrary indices, what else *could* you do but replace each of the 4 elements individually?  The logic it would take to check for and identify the (probably) rare special cases where a subset of the 4 happen to go to consecutive indices would almost certainly cost more time than it would save, and would also cost significantly more time to develop and debug.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
0 Kudos
Message 2 of 14
(1,730 Views)

@siddharth9678 wrote:

Hello Team,

 

There is an input array of size 4 which is fed as an input to a for loop. With in loop, i have an zero initialized array of size 100, in each iteration have to assign the input array at 4 different indices....so basically loop runs 25 times and in each iteration input array of size 4 gets assigned at 4 different indexes of 100 size array.

 

lets say iteration 1, indexes are 1,9,11,20 and data is D0,D1,D2,D3. So D0 is replaced at index1, D1 is replaced at index 9 and so on

 

Simplest way to implement is 4 array replacements per iteration....Is there any efficient way to implement it in a single replacement array
operation or min replacements per iteration. 1 efficient way is to create a mask and then use it to implement the particular case. 

 


We typically do much better looking at some LabVIEW code instead of long ambiguous descriptions.

 

So you have an array of four elements and these elements don't change during the execution of the loop.

Then you have an array of size 100, filled initially with zeroes.

With each iteration, four of the zeroes are replaced depending on some other input that has not been mentioned. Is that a 2D array with 25 rows, each row containing four indices?

After four elements have been replaced, should they remain for the next iterations or should the next iteration start from an array of zeroes again? If they remain, what should happen if an index occurs a second time?

Is that array temporary (to be processed in the loop) or should it form a 2D array with 25 rows and 100 columns?

 

What are ALL the inputs? What is the desired final result?

 

What is the purpose of all this? Maybe there is an entirely better way to do whatever you are trying to do.

0 Kudos
Message 3 of 14
(1,694 Views)

Not really what you're asking about, but I find it handy sometimes.

0 Kudos
Message 4 of 14
(1,647 Views)

@paul_a_cardinale wrote:

Not really what you're asking about, but I find it handy sometimes.


Enlarging an array by padding an array with default elements is typically simpler with "reshape array".

 

altenbach_0-1681745482093.png

 

Message 5 of 14
(1,639 Views)

@altenbach wrote:

@paul_a_cardinale wrote:

Not really what you're asking about, but I find it handy sometimes.


Enlarging an array by padding an array with default elements is typically simpler with "reshape array".


That's a nice little trick! I had forgot about that!

"

If the product of the dimension sizes is greater than the number of elements in the input array, the function pads the new array with the default of the data type of n-dim array."
G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 14
(1,634 Views)

Personally, I would use flat code (reshaping to the same size is a noop, i.e. free). It should also handle array inputs.

 

Here's a "non-vim" draft.

 

altenbach_0-1681748104889.png

 

 

Similarly, a vim could be made to work with arrays of higher dimension.

 

 

 

 

0 Kudos
Message 7 of 14
(1,620 Views)

@altenbach wrote:

Personally, I would use flat code (reshaping to the same size is a noop, i.e. free). It should also handle array inputs.

 

Here's a "non-vim" draft.

 

altenbach_0-1681748104889.png

 

 

Similarly, a vim could be made to work with arrays of higher dimension.

 

 

 

 


Here it is as a .vim.  Adding more dimensions gets complicated: suppose you're placing  1D subarray into a 2D array; now you have to specify orientation (insert as a column, or row).  I don't even want to think about higher dimensions.

0 Kudos
Message 8 of 14
(1,585 Views)

Thanks for the reply....Sorry for not sharing labview code as i didnt start doing so. 

Sorry for the confusion created by ambigous description. Long story short ...what i want is lets say i have an array of initialized 0s of size 100 and i have data elements d0,d1,d2,d3 to replaced at index 1,9,11,20 in zero initiazed array. Can this be done in an efficient manner than having 4 array replacement operations.

 

Still i have described my earlier problem statement....

"After four elements have been replaced, should they remain for the next iterations or should the next iteration start from an array of zeroes again? If they remain, what should happen if an index occurs a second time?"

They remain for next iterations after four elements has been replaced per iteration.

No of iterations can be any thing between 1 to 25. No index occurs second time.

 

 Scenario is this...Total no of data elements is known in advance.

Let say there are 100 data elements D0,D1....,D99. Now for my case i am dividing this data in bunch of 4 datas each time/iteration.

Lets say no of iterations = 2

1st iteration data D0,D1,D2,D3 at indices 1,9,11,20

2nd iteration data D4,D5,D6,D7 at indices 2,25,48,51

 

Basically there is a table where i have indices mapped to the iteration value.

 

Since size of the data is known in advance, i have created zero initialized array of size 100 where in at indices 1,9,11,20 i will have D0,D1,D2,D3 and at 2,25,48,51 i will have D4,D5,D6,D7.At remaining indices all will be 0. Main thing i want help is  "4 array replacements per iteration" if this can be optimized in terms of array replacement. As far as i know it may not be optimized further...Still wanted to take opinion of experts on this.

 

Similarly iterations can be 25, where all 100 indices will be occupied.

 

Thanks

0 Kudos
Message 9 of 14
(1,582 Views)

See my earlier msg #2.  Since you aren't gong to be resizing the array, replacing elements will be extremely fast.  You really can't do any appreciable optimization.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
0 Kudos
Message 10 of 14
(1,557 Views)