LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

3 loops incrementing

Solved!
Go to solution

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]

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

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 10
(3,683 Views)

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.  

Message 2 of 10
(3,672 Views)

Hi alten,

 

That sounds like a great idea, but can you elaborate?  I don't understand it completely.

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

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

jyang,

 

See attached file

 

Rgs,

 

Lucither

------------------------------------------------------------------------------------------------------
"Everything should be made as simple as possible but no simpler"
Message 4 of 10
(3,644 Views)

Hi jyang,

 

there might be more efficient/nicer ways to combine 4 elements of 2 arrays, but you may use an approach like this:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 5 of 10
(3,627 Views)

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!

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

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 6 of 10
(3,576 Views)

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.

 

CartesianProductEx.png

 

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.

 

CartesianProductElement.png

 

(Add futzing with label sizing to the list of things that Snippets do)

Message 7 of 10
(3,564 Views)

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 Smiley Tongue

 

Rgs,

 

Lucither.

------------------------------------------------------------------------------------------------------
"Everything should be made as simple as possible but no simpler"
0 Kudos
Message 8 of 10
(3,544 Views)

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

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

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 9 of 10
(3,513 Views)

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

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

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 10 of 10
(3,511 Views)