LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Overwrite array in loop

Solved!
Go to solution

Hello, I want to ask if it is possible to change a value in an array in a loop and then use it again as the input. What I want to do is use an array as an input to a loop, change some values in it and then run the same loop again, but with the output array as an input. I want to run this loop until a certain condition is met in the array.

 

Any help is appreciated!

0 Kudos
Message 1 of 16
(3,401 Views)

Use Shift Registers and Arrays functions.

-------------------------------------------------------
Control Lead | Intelline Inc
Message 2 of 16
(3,377 Views)

Did you understand @ZYOng's response?  Here's a specific example:  Start with an Array of 100 elements, all zero.  Put it into a While Loop and replace the first 0 element with 1, then the second zero element with 2, and stop when you have replaced the last zero element with 100.

 

What do you need?  First, you need an Array of 100 elements, all 0.  That's the "Initialize Array" function.  Next you need a While Loop, with a Shift Register (bring the output from Initialize Array to the left edge of the While Loop -- it will create a "Loop Tunnel", a solid square the same color as the wire.  Right-click it, and "Replace with Shift Register".  Now, inside the loop, you want to replace the i-th element with "i+1" (Array indices start with 0, but we want to put "1" in the first ("0-th") element).  How do you replace an element in an existing Array?  Use "Replace Array Subset", with Array In and Array Out coming from and going to the Shift Registers, the "index" being "i", the Loop Index, and the "new element" being "i+1", which I code by using the "Increment" function and the existing "i" wire as its input.  When do we stop this loop?  When (i+1) = 100.  (Note that the preceding step gives us (i+1) on a wire, so we only need a "Equal?" function with i+1 on one input and 100 on the other).  Click the right-hand Shift Register and "Create Indicator", then run this to see (that/if) it works.

 

This is simple enough I'm not going to include a picture, much less a snippet.

 

Note, also, there are other (better?) ways to generate an array of 1..100.  One is to use a For Loop instead of a While, since the indexing conditions are "Do this for every element in sequence.  Less-well-known is to use an "Indexing Tunnel" to generate the 100-element Array, doing away with the Array functions and the Shift Register.  Here's how to "Have Your Cake and Eat It, Too":

 

  1. Start with the Block Diagram you just built, with Initialize Array, While Loop with Shift Register and Replace Array Subset. 
  2. Look at the wire that carries "i+1" to the "new element" input, and create another wire from this wire to the right edge of the While Loop (where it again becomes a Loop Tunnel). 
  3. Right-click the Tunnel, choose "Tunnel Mode", and change it from "Last Value" to "Indexing".  Recognize that symbol?  The "inner piece" is a pair of square brackets "[]", a common symbol for Array elements in other Programming Languages. 
  4. Is this the same Array we just created (that is also coming out of this While through the Shift Register?  Drop another "Equals?" function and wire the two Arrays into it.  The output will be a Boolean Array of 100 elements, one for every pair-wise comparison of the corresponding Array elements. 
  5. Are they all the same?  Look at the Boolean functions.  Do you see the one with the upside-down A (the symbol for "Universal", or "All True")?  Wire the Array to its input, and a Boolean indicator to its output.  Run this VI -- the indicator should light up, meaning both methods generated identical values for all elements.

Bob Schor

Message 3 of 16
(3,320 Views)

@eeeeeeeeeeehelp wrote:

Hello, I want to ask if it is possible to change a value in an array in a loop and then use it again as the input. What I want to do is use an array as an input to a loop, change some values in it and then run the same loop again, but with the output array as an input. I want to run this loop until a certain condition is met in the array.


If the array should change inside the loop across iteration, keep it in a shift register. However, you also say "run the same loop again" and for that you would also need a external feedback node or a shift register in a outer loop. (the exact implementation depends on factors we don't know yet!)

 

Can you explain what you mean by "run again"? Stop the VI and run it again? return to the state containing this inner loop in a state machine architecture? Call this loop (located in a subVI) again from the main VI? What should the array content be on the first run? Wire does the new data come from? What are the conditions?

 

Why don't you explain what problem you are trying to solve and maybe attach your VI attempt and we could give you much more specific advice.

0 Kudos
Message 4 of 16
(3,303 Views)

@Bob_Schor wrote:

Did you understand @ZYOng's response?  Here's a specific example:  Start with an Array of 100 elements, all zero.  Put it into a While Loop and replace the first 0 element with 1, then the second zero element with 2, and stop when you have replaced the last zero element with 100.

 

What do you need?  First, you need an Array of 100 elements, all 0.  That's the "Initialize Array" function.  Next you need a While Loop, with a Shift Register (bring the output from Initialize Array to the left edge of the While Loop -- it will create a "Loop Tunnel", a solid square the same color as the wire.  Right-click it, and "Replace with Shift Register".  Now, inside the loop, you want to replace the i-th element with "i+1" (Array indices start with 0, but we want to put "1" in the first ("0-th") element).  How do you replace an element in an existing Array?  Use "Replace Array Subset", with Array In and Array Out coming from and going to the Shift Registers, the "index" being "i", the Loop Index, and the "new element" being "i+1", which I code by using the "Increment" function and the existing "i" wire as its input.  When do we stop this loop?  When (i+1) = 100.  (Note that the preceding step gives us (i+1) on a wire, so we only need a "Equal?" function with i+1 on one input and 100 on the other).  Click the right-hand Shift Register and "Create Indicator", then run this to see (that/if) it works.

 

This is simple enough I'm not going to include a picture, much less a snippet.

 

Note, also, there are other (better?) ways to generate an array of 1..100.  One is to use a For Loop instead of a While, since the indexing conditions are "Do this for every element in sequence.  Less-well-known is to use an "Indexing Tunnel" to generate the 100-element Array, doing away with the Array functions and the Shift Register.  Here's how to "Have Your Cake and Eat It, Too":

 

  1. Start with the Block Diagram you just built, with Initialize Array, While Loop with Shift Register and Replace Array Subset. 
  2. Look at the wire that carries "i+1" to the "new element" input, and create another wire from this wire to the right edge of the While Loop (where it again becomes a Loop Tunnel). 
  3. Right-click the Tunnel, choose "Tunnel Mode", and change it from "Last Value" to "Indexing".  Recognize that symbol?  The "inner piece" is a pair of square brackets "[]", a common symbol for Array elements in other Programming Languages. 
  4. Is this the same Array we just created (that is also coming out of this While through the Shift Register?  Drop another "Equals?" function and wire the two Arrays into it.  The output will be a Boolean Array of 100 elements, one for every pair-wise comparison of the corresponding Array elements. 
  5. Are they all the same?  Look at the Boolean functions.  Do you see the one with the upside-down A (the symbol for "Universal", or "All True")?  Wire the Array to its input, and a Boolean indicator to its output.  Run this VI -- the indicator should light up, meaning both methods generated identical values for all elements.

Bob Schor


 

Wow! I wasn't expecting a response this detailed and easy to understand. For some reason I was under the impression that shift registers can only work with single values and that they can't work with arrays. This helps me a lot, thank you. I was trying to do something similar before and managed to work around it by building a whole new array instead, I can use this to simplify my earlier code too.

0 Kudos
Message 5 of 16
(3,288 Views)

@altenbach wrote:

@eeeeeeeeeeehelp wrote:

Hello, I want to ask if it is possible to change a value in an array in a loop and then use it again as the input. What I want to do is use an array as an input to a loop, change some values in it and then run the same loop again, but with the output array as an input. I want to run this loop until a certain condition is met in the array.


If the array should change inside the loop across iteration, keep it in a shift register. However, you also say "run the same loop again" and for that you would also need a external feedback node or a shift register in a outer loop. (the exact implementation depends on factors we don't know yet!)

 

Can you explain what you mean by "run again"? Stop the VI and run it again? return to the state containing this inner loop in a state machine architecture? Call this loop (located in a subVI) again from the main VI? What should the array content be on the first run? Wire does the new data come from? What are the conditions?

 

Why don't you explain what problem you are trying to solve and maybe attach your VI attempt and we could give you much more specific advice.


 

I am trying to do HDB3 encoding, I won't have access to my work pc for a few days so I can't post my VI right now. But specifically, I have an array of voltage values that each represent one binary bit, and whenever there are 4 zeroes in a row, then I need to replace them with either 1001, or 0001. I was thinking of finding the first 4 zeroes, replacing them with the 4 bits, breaking the loop and then running the loop again, until it doesn't find any 4 zeroes in the array after each other.

0 Kudos
Message 6 of 16
(3,281 Views)

@eeeeeeeeeeehelp wrote:

I have an array of voltage values that each represent one binary bit, and whenever there are 4 zeroes in a row, then I need to replace them with either 1001, or 0001. I was thinking of finding the first 4 zeroes, replacing them with the 4 bits, breaking the loop and then running the loop again, until it doesn't find any 4 zeroes in the array after each other.

 

You don't need to break the loop after each replacement. all replacements can be done in successive iterations. Just keep track of the last replacement position.

 

Who decides if 0000 should be replaced by 1001 or 0001?

 

It is sufficient to check them in adjacent groups of four (0,1,2,3},{4.5.6.7}, {8,9,10,11}, etc. or for all indices, i.e. {0,1,2,3}, {1,2,3,4}, {2,3,4,5}, etc. ?

 

Al alternative would be to to change it to one long string of 0, 1 characters and use string functions. 

0 Kudos
Message 7 of 16
(3,268 Views)

@altenbach wrote:

@eeeeeeeeeeehelp wrote:

I have an array of voltage values that each represent one binary bit, and whenever there are 4 zeroes in a row, then I need to replace them with either 1001, or 0001. I was thinking of finding the first 4 zeroes, replacing them with the 4 bits, breaking the loop and then running the loop again, until it doesn't find any 4 zeroes in the array after each other.

 

You don't need to break the loop after each replacement. all replacements can be done in successive iterations. Just keep track of the last replacement position.

 

Who decides if 0000 should be replaced by 1001 or 0001?

 

It is sufficient to check them in adjacent groups of four (0,1,2,3},{4.5.6.7}, {8,9,10,11}, etc. or for all indices, i.e. {0,1,2,3}, {1,2,3,4}, {2,3,4,5}, etc. ?

 

Al alternative would be to to change it to one long string of 0, 1 characters and use string functions. 


I have to check it for all indices, as to if the 0000 should be replaced with 0001 or 1001, I think that I should be able to do that myself. But it depends on the number of 1s before the 4 zeroes, if the number of 1s is odd then it gets replaced with 0001, if it's even then 1001 (the idea is to keep the number of 1s even). Technically you should only count the 1s between the last 4 zeroes and the current 4 zeroes, but I think it would be easier to break the loop and count the ones from the start, since you get the same result anyway.

 

Now that I'm writing this, I might be confusing loops with iterations, well I won't know until I try to make the actual code.

0 Kudos
Message 8 of 16
(3,229 Views)

@eeeeeeeeeeehelp wrote:
I have to check it for all indices, as to if the 0000 should be replaced with 0001 or 1001.

Who makes the decision about the replacement value? You can only pick one of the two!

0 Kudos
Message 9 of 16
(3,225 Views)

@eeeeeeeeeeehelp wrote:

Hello, I want to ask if it is possible to change a value in an array in a loop and then use it again as the input. What I want to do is use an array as an input to a loop, change some values in it and then run the same loop again, but with the output array as an input. I want to run this loop until a certain condition is met in the array.

 


As has been mentioned several times, the basic principle is a shift register. Here is a very primitive example that takes an array of zeroes and always replaces the smallest value with a new random number until the array sum is >9.

 

altenbach_0-1700586224905.png

 

 

In your case you would take successive array subsets of four element starting at index 0 and conditionally replace it with new values as required.

 

 

0 Kudos
Message 10 of 16
(3,222 Views)