LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

insert elements into array

Hi Guys,
I am having a little problem with inserting elements into an array. (I think I've been looking at it for too long and need a fresh approach).
I have an two arrays:
One is my array of data and the other holds the indices of where I wish to insert/replace the data.
My array is made up of 1's and 0's and I wish to insert/replace with a 0 at the specified indices in array2.
Also there is some bug in my code when I am collecting the indices.
Here is what I have (I know this looks very trivial but I just cant figure this out!!!)
Thanks,
Maria


0 Kudos
Message 1 of 13
(4,282 Views)
can you save it for LV7.1? 😛
0 Kudos
Message 2 of 13
(4,266 Views)
Hi
Sorry cant save it as LV 7.1. I only have the 8.0 evaluation software
I'll attach a picture of the one I am working on at the minute if that helps.
Thanks for replying.

Message Edited by MariaIre on 03-24-2007 12:21 PM

Message Edited by MariaIre on 03-24-2007 12:22 PM

0 Kudos
Message 3 of 13
(4,265 Views)
i don't know if i understood what you want to do, but i make this vi

i hope it will help you 😉


0 Kudos
Message 4 of 13
(4,260 Views)
Hi thanks for your prompt reply. Your VI only replaces the first element it comes across. May be I didn't explain it well.
In array there are 0's and 1's. I want to get the indexes of the 1's in array and then replace the data at these elements in array two. Your VI did this but it only did it for the first index  of a 1. Do I need to put this into the index array into some type of loop??

0 Kudos
Message 5 of 13
(4,255 Views)
Your code makes little sense. Please add some typcal data and expected results to all your controls and indicators, set all current values to defaults, resave, and re-attach your VI. Thanks!
 
You have serious conceptual problems in your code:
  1. Left loop.
    • You loop for each element in the original array (+1!, [i] starts counting at zero!), where you actually only need to loop for the total number of "1s".
    • The "search array" node will tell you when you run out of elements.
    • The use of the feedback node is mindboggling. You actually get one too many elements in the output array. And since you are not initializing it, everything will be off except during the first run.
  2. Right Loop
    1. Here you need a FOR loop because the iteration count is determined from the beginning.
    2. If you use "insert into array", you are constantly resizing the array AND the indices of all elements above the insert point will increment. This will mess with the indices and makes it difficult to write predictable code. Think about it!
    3. You are starting out with an empty array, then try to insert at random locations. Makes no sense.
    4. All that song and dance with array size and comparisons is not needed in a FOR loop.
  3. Overall
    1. Keep your representations straight, e.g. don't add a DBL indicator for a I32 array.
    2. Use reasonable labels for indicators (array, array 2 and array 3 are too generic).
    3. There is a big difference between "insert into array" and "replace array element".
      • The first grows the array size, shifting all higher indices, requiring extra datacopies and expensive memory reallocation operations.
      • The second keeps the array at the same lenght, replacing certain elements with new data. This operation is "in place" and thus efficient.
    4. Your quote: "I wish to insert/replace with a 0 at the specified indices in array2" tells me that you don't seem to appreciate the difference.

So, things are probably much simpler than they seem at the moment. 🙂

Message Edited by altenbach on 03-24-2007 11:31 AM

Message 6 of 13
(4,253 Views)
sorry, i make some mistakes and didn't test the vi 😛

i think it works like you want 😉


Message 7 of 13
(4,248 Views)

If you just want to zero element at corresponding locations where the "PC..." array is 1, you don't need to generate the array of indices, you don't even need a loop.

Due to the inherent potential problems comparing DBL values, the "PC ..." array should probably be integer. It only contains integer values anyway. Right?

Assuming that the "PC..." and data Array are of equal size, you can for example use a simple loopfree solution as shown in the picture. If you really also want an array output showing the replaced indices, you could do something like is shown in the optional part of my code.

Message Edited by altenbach on 03-24-2007 12:57 PM

0 Kudos
Message 8 of 13
(4,245 Views)
Altenbach,
      Are you sure you want to search for a zero in the for-loop?
 
Cheers.
 
"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 9 of 13
(4,213 Views)


tbd wrote:
      Are you sure you want to search for a zero in the for-loop?

Yes! Why not? 🙂

(We do a !=0, so all replacements will have a zero in the 0:1 array. The 1s are the elements we did not touch. We don't care about those. ;). Remember, I search the "?0:1" array, not the original array.)

 

0 Kudos
Message 10 of 13
(4,206 Views)