01-31-2011 05:39 PM
I am making a subvi. The input is three seperate 1D array. Array 1 has 2 elements. Array 2 has 2 elements. Array 3 has 1000 elements. If I were to write a triple loop, I would do the following
Loop 1 through array 1
Loop 2 through array 2
Loop 3 through array 3
End
End
End
Array 3 will be iterated through 4 times. However, I don't want to write a triple loop. If I do that, my program will be trapped inside the loops for a long time. I am trying to break the loop into a state in a state machine that will get called mutliple of times with different values from the array. Thus, I need to write a subvi that behaves like the triple loop, but it would only execute once every time it is called. See below for the iteration. I am trying to do something different than what I would usually do, so I am having a brain freeze. Give me a pointer if you can. Thanks!
Array1[0], Array2[0], Array[0-999]
array1[0], array2[0], array [0]
....
array1[0], array2[0], array [999]
Array1[0], Array2[1], Array[0-999]
array1[0], array2[1], array [0]
....
array1[0], array2[1], array [999]
Array1[1], Array2[0], Array[0-999]
array1[1], array2[0], array [0]
....
array1[1], array2[0], array [999]
Array1[1], Array2[1], Array[0-999]
array1[1], array2[1], array [0]
....
array1[1], array2[1], array [999]
Solved! Go to Solution.
01-31-2011 06:35 PM
Create a linear index with a size of the product of all dimensions and increment it in a shift register. Use two successive quotient&remainder on it to calculate the three indices and use them to index into your three arrays.
01-31-2011 11:50 PM
Hi alten,
That sounds like a great idea, but can you elaborate? I don't understand it completely.
02-01-2011 12:03 AM - edited 02-01-2011 12:07 AM
jyang,
See attached file
Rgs,
Lucither
02-01-2011 02:16 AM
02-03-2011 04:18 PM
Lucither and Alten,
How do you come up with a algorithm like this? I am looking at it, and it makes sense to me generally, but I am still trying to put my arms around it. Any hints and method to get myself to think like that? Thanks!
02-03-2011 05:03 PM
This was very nearly my weakly nugget this week, and would have been next week, but here is a spoiler. A hidden gem in vi.lib will do much of the work for you once you realize where to look and what you are looking for. This is a very common operation, called the Cartesian product, of your arrays. There is a VI which will return the indices of this product for you. All you need is this.
For an inplace implementation, I use this subVI which returns the indices for a given iteration. You can add range checking if you like, I choose not to since this is sometimes used in tight loops and I set the limits beforehand.
(Add futzing with label sizing to the list of things that Snippets do)
02-03-2011 09:15 PM
jyang wrote:
How do you come up with a algorithm like this? I am looking at it, and it makes sense to me generally, but I am still trying to put my arms around it. Any hints and method to get myself to think like that? Thanks!
One of the best things that has helped me with LabVIEW in particular is this forum. I look at interesting problems posted and see how the many experts on here solve them. Over time some of it has rubbed off. The NI forum for me is by far the best technical forum i use. There are a lot of helpful, knowledgeable people on here all with different techniques and methods to solve a problem. I tend to look at them all and steal the ideas i like for myself
Rgs,
Lucither.
02-04-2011 10:19 AM
Hi Darin,
Thank you for telling me about the Cartesian product. Either I have never heard of it, or I gave it back to my professor. I believe that it will be a very useful tool.
Yik
02-04-2011 10:21 AM
Hi Lucither,
I agree with you. How do you pick which post to read? A lot of times, when I use this forum, I just post questions, read obvious great material, like the nuggets, and answer easy questions. I feel like I am missing out on a lot.
Yik