LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

compare and replace value

Solved!
Go to solution

Hello,

 

Just need a quick help. I have 3 arrays. I have some data in Array1 wich has 10 columns.

I am taking the value in the first columns of Array1 and compare them with the values in Array 2. For example, from array 1, if 133.38 is between 30.00 and 230.00 (of arry 2), then put 50.0 (of array 3) in the last column of Array1.

It means simply, take each value in Column1 of Array1, check in array 2 the range of that value, and then outpu in the last column of array 3 the value in the index of array 3 corresponding to the index of the lower limit.

Example 2. considering 400.00 in column1 of array1, compare its range with the value in array2. we can see that 400 is between 230 and 1000.00 (in array2), so then we have to put 70.0 in the last culomn of Array1.

Thank you for this help.

 

0 Kudos
Message 1 of 5
(5,156 Views)

Nice homework problem.  Sometimes it helps to "divide and conquer".

 

First, think carefully about the first part of your problem.  You have an Array (2) -- are you guaranteed that it is in ascending order?  If presented with a number and Array 2, what output do you want?  Think about all the cases (let's assume that the array is "equal or ascending") -- what if your number is less than all the number in the Array?  Greater than all the numbers?  Equal to one of them?  Any other case to consider?

 

Write a little bit of LabVIEW code to encapsulate this bit of the problem.

 

Now you have a value, and another Array (3).  What do you want to do with your value and the Array?  Will you always be able to return a number?  Write down this little bit of logic.  How does it "connect" with the earlier bit you did?

 

OK, so where did we get the original number we used with Array 2?  What do we want to do with the number we get from Array 3?

 

Put everything together, and try it out.  Does it work?

 

If you do it yourself, you will learn something.  IF ONE OF US DOES IT FOR YOU, you will learn How to Cheat.

 

Bob Schor

0 Kudos
Message 2 of 5
(5,145 Views)
Solution
Accepted by topic author agyna

Use Threshold 1D Array to get the index of where your first value will be relative to array 2, round down (toward -Infinity) and index from Array 3.  Relplace the corresponding cell in array 1.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 5
(5,141 Views)

I would  do the same as Tim, but since you want to modify array 1, just write to a local variable of it at the end.

 

Depending on the quality of the data, you might also need to handle some special cases and Bob already hinted to some of them.

You need to define the output if e.g. the initial value is outside, e.g. zero or 2000. What if the value is an empty string or not numeric?

What if the 2D input array only has 9 columns initially. Now Tims' code would fail because you cannot replace what does not exist.

You need to make the code bulletproof so LabVIEW can tell if something is wrong and return an error.

 


Now let's look at your original program (ignoring all program logic and just looking at the general coding techniques!):

  • All array terminals belong on the toplevel diagram. Never place them inside a loop unless you expect the values to change during the execution of the loop (which would probably invalidate all the results). Having LabVIEW re-read all terminals with each iteration is just a big waste of resources. If the terminals are before the loop, LabVIEW can treat the values as constants during loop execution which is much more efficient.
  • In the inner loop, you are constantly indexing out the same element of "array 3". Again, this belongs before the loop. Now you have an index array wired to [i], which is often (not always!) equivalent to autoindexing on the array
  • Index array is resizeable, so you only need one instance to get  the element at i and i+1 in the inner loop, and you only need to wire the upper index. The problem is that you are indexing outside the array on the last iteration for the i+1 element, so your result is probably not as expected.
  • If you are modifying the 2D array in the shift register inside the inner loop, you probably also need a shift register on the inner loop, else only the value from the last iteration is retained (and since you are indexing outside the array in the last iteration, all bets are off! :D)
  • I have no idea what the "initialized array" is supposed to do, but to convert a scalar into a 1D array with one element, a simple "built array" of height 1 is sufficient. It is also not good to place an indicator inside an inner case, because you never know where the data came from. For example if the case is always false, the indicator might contain a stale value from a previous run.
  • Your input array elements has non-default defalut values (A string of "1000.00"). The default should be an empty string instead.
  • ...

 

 

0 Kudos
Message 4 of 5
(5,108 Views)

Thanks a lot. Simply efficient.

0 Kudos
Message 5 of 5
(5,088 Views)