01-27-2016 12:14 AM
Hi
I want to delete every 4th element in an 1D array.
I am attaching my vi.
I understand I am not getting my expected result since I am keeping my FALSE case unwired...but I am not getting any workaround.
Plz help
01-27-2016 01:50 AM - edited 01-27-2016 01:52 AM
Hi ni4me,
you need to learn several things:
- usage of shift registers (to keep values from one iteration of a loop to the next iteration)
- wiring the FALSE case of a case structure - instead of "use default value if unwired"!
When you have taken those steps you will notice your deletion algorithm is flawed: after deletion of index 4 the remaining elements are changing their indices: element 5 becomes element4, 6 to 5, and so on. But you still keep on counting like everything is still in place…
Solution: Start deleting at the end of the array!
When your arrays will always have a size a multple of 4 you could try this instead:
01-27-2016 02:08 AM - edited 01-27-2016 02:11 AM
A possible solution, the case structure have a default case and 0 case . on the defaul wire the array withouth nothing else, in the 0 case you add the element on the new array.
01-27-2016 04:47 AM - edited 01-27-2016 04:47 AM
@ByteLABS wrote:
A possible solution, the case structure have a default case and 0 case . on the defaul wire the array withouth nothing else, in the 0 case you add the element on the new array.
Better yet: Conditional Indexing Tunnels!
01-27-2016 05:00 AM
If this code runs often and the array is large, it may be more memory efficient to initialize an array first and copy the original array's elements (the ones that you want to keep) in the for loop. Since you know how many elements you expect to keep, you only have to initialize your array once (whereas the conditional indexing tunnel has to re-allocate memory for the array each time you append a new element).
01-27-2016 12:14 PM
@berkinorbi wrote:
If this code runs often and the array is large, it may be more memory efficient to initialize an array first and copy the original array's elements (the ones that you want to keep) in the for loop. Since you know how many elements you expect to keep, you only have to initialize your array once (whereas the conditional indexing tunnel has to re-allocate memory for the array each time you append a new element).
The Conditional Indexing Tunnels in a FOR loop do exactly as you just described.