LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can't build array without indexing?

Hi all,

I'm getting a bit frustrated here as I want to build a 1D array of values, the length of which is unknown, with each iteration of a case statement within a loop. It seems as though if you don't know the length of the array you want to build this can be impossible because you cannot "index" the array unless you exit the loop, which isn't an option. As I said, I don't know what length the array will be and I don't want to be inefficient and just initialize a very large array.
0 Kudos
Message 1 of 5
(3,274 Views)
You can canditionally built the array in a shift register.
0 Kudos
Message 2 of 5
(3,270 Views)
Here's a quick example how you could do it. The following code makes an array of all random numbers that are > 0.5. See if it makes sense.
 
 
Just ignore the mispellings of the objects. 😉
 
(If the array will be huge, there are more efficient ways to do this, though.)


Message Edited by altenbach on 04-26-2008 08:47 AM
0 Kudos
Message 3 of 5
(3,265 Views)

FiberOptix wrote: As I said, I don't know what length the array will be and I don't want to be inefficient and just initialize a very large array.
"Inefficient" is an interesting word here, because you might have some misconceptions. Allocating a large array is very efficient compared to growing an array incrementally in a loop. So, if you can guess an reasonable upper limit for the size, allocate that (or even twice that!) and then do everything "in place" inside the loop, it will be much more efficient than growing the array with most iterations. Every time you resize an array, a new copy of the array needs to be made in memory, so that can become very expensive in the long run.
 
For an illustrative example, have a look at code alternatives that remove certain elements from a large array. Using "delete from array" (a resizing operation!) will become exceedingly expensive. Have a look at the post here. For an array with 500000 elements, it can mean the difference between 60 seconds and 12milliseconds! and for a 10x larger array the difference is estimated to be 100minutes(!!) vs. 120ms, or about the blink of an eye compared to an extended lunch break. 😮 (Please read the entire thread for more specific discussions). Similar rules apply for growing arrays as in your case.
 
Unless your array will be 100's of megabytes and you run into memory and OS limitations, memory is typically not an issue.
0 Kudos
Message 4 of 5
(3,256 Views)

Thank you for your very informative replies Altenbach. I almost hate to say it but not long after I posted I found a more elegant way to do what I wanted to do without the use of arrays. I'll keep your post handy though, because I've run into this problem before.

Thanks again!

 

Ryan

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