LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Rebuild array efficiently

Solved!
Go to solution

Hi all,

 

I have an array of 10 element, for example.  I need to delete a few elements inside the array if the elements fulfill a criteria.  I know how to do that, but I am not sure is it the must efficiently of doing so, since I will need to use the function "build array" in a loop, and I know that it is a big no no, if I can help it. 

 

I attached an example my problem and my approach.  Notice that the condition used to select which element to select from the input array is just an example.  The condition could be anything. 

 

Alternative apprach I thought about (I guess I can code up some stuff, but I would like to hear your suggestion first):

 

1. I thought about using delete from array, but I thought that it will be messy, since once I delete one element, the indexing would mess up a little. 

2. I thought about initializaing an array of a fix size and replace the element from that array with elements selected from the other array, but not sure it is worth it to go through the additional trouble. 

 

3. It will be nice if there is a function that will output an array of index of all the elements in an input array that fit a criteria.  I know that there is a function that will do that but it is only for one element. 

 

4. Also, I will be even nicer if there is an delete from array function that would take an array of index, delete multiple elements from an array at the same time.

 

Yik

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 5
(3,316 Views)
Solution
Accepted by topic author jyang72211

See this discussion.

 


jyang72211 wrote:

4. Also, I will be even nicer if there is an delete from array function that would take an array of index, delete multiple elements from an array at the same time.


 

"Delete from Array" function should accept an array at index Smiley Wink

Message Edited by smercurio_fc on 03-10-2010 03:44 PM
Message 2 of 5
(3,307 Views)

Here is how I do it. This was to monitor connections. I checked if the refnum was valid, if was, I used replace array subset to insert it in the array. If it wasn't valid, I didn't add it in. Each time an entry was added, i increased my counter in the shift register. This stored the number of elements added to the array. Then at the end I reshaped the array to trim off the extra. Ignore the while loop here, this was in an action engine. In the true case inside the for loop, nothing happens, the wires are just wired straight through.

 

For your case rather than check if a refnum is valid, you may check the element in the array. If you wanted to remove all 3's in the array you would wire = to 3 up to the case structure.

 

Message Edited by for(imstuck) on 03-10-2010 03:49 PM
Message 3 of 5
(3,302 Views)

There are two approaches to this basic (fundamental) problem.  Note however, especially if the array sizes are so small (10 elements), then I would not worry too much about how it is done but as long as it works.

 

1.  Delete elements from the original array according to Condition.

 

2.  Insert elements into a new array based on the inverse Condition (example image attached).

 

In both of these cases you will need to keep track of the current array index by using a shift register.

 

insert into array.png

Message 4 of 5
(3,293 Views)


There are two approaches to this basic (fundamental) problem.  Note however, especially if the array sizes are so small (10 elements), then I would not worry too much about how it is done but as long as it works.

 


True, but I would be careful because you never know when functionality in the future may change and array sizes will need to grow. It is better to assume sometime the program will be made to hold thousands of array values, and design it that way. That way you don't have to modify more stuff down the road.

0 Kudos
Message 5 of 5
(3,290 Views)