From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

compare 2 1D arrays of doubles

I have 2 1D arrays of double values

 

image.png

I would like to convert each of them to integer values (val1=373150 val2=373146) so that I can compare which is larger.  For this case val1 will be larger than val2.  

0 Kudos
Message 1 of 9
(2,682 Views)

Are there always 5 elements in the array?  Are they always single digits except for the 4th value which is two digits?  If so,  Just to a greater than comparison and a less than comparison of the two arrays.  Which ever one as the first True element from Search 1D array will be the larger.

 

Assuming the problem isn't that well defined.  Convert the number array to a string array.  Concatenate the string arrays.  Convert the resulting two strings back to numbers and compare.

0 Kudos
Message 2 of 9
(2,642 Views)

@Eth wrote:

I have 2 1D arrays of double values

 

image.png

I would like to convert each of them to integer values (val1=373150 val2=373146) so that I can compare which is larger.  For this case val1 will be larger than val2.  


I have no idea what you want to do, nor do I understand why it is difficult for you.  Oh, I just "got it" -- you are concatenating the rounded-integer value of each entry (essentially "packing digits together", which is something I've never seen before (and can't imagine why you'd want to do it.

 

OK, as often happens, pencil and paper (or keyboard and fingers, in my case) is often a good way to start.  Here is an Algorithm:

  1. Create a For Loop with a Shift Register initialized with an I32 value of 0.  Pass an Array of Dbl into it.
  2. For each number, round to nearest integer and convert it to I32 (you can probably just do the Conversion, but if you want to know or control the rounding, doing two steps is pretty cheap in terms of time and effort).
  3. Now you need to worry about how many digits your number has, as you'll have to shift the accumulating value in the Shift Register accordingly.  Write a sub-VI to do this, something that takes the Integer as input and returns a Power-of-10 as the output -- it will make your project "neater" and simpler to understand.
    1. You can do this with a While loop, or if you like recursion (as I do), you can write a recursive function.
    2. Here's the notion -- start with a While Loop with two Shift Registers, one with N (your number), the other with 1 (a Power of 10).
    3. Inside the loop, replace N with N Div 10 (use the Quotient/Remainder function), replace Power-of-10 by 10 * Power-of-10.  If the Quotient is 0, stop the loop.
    4. Use the output Power-of-10.  [Convince yourself that this algorithm works].
  4. So now you multiply the number in the Shift Register (the "Accumulator" by the Power-of-10 (shifting it that many places to the left) and add the digits from the Array.  When you process all of the Array entries, you'll have your Value.

     Now that you have an algorithm, code it in LabVIEW.

 

Bob Schor

 

0 Kudos
Message 3 of 9
(2,640 Views)

These look more like integers than DBLs.

 

What should happen if the number of digits differs for a pair of elements?

 

Can't you just compare elements (after verifying equal array size) in a loop or otherwise? Seems more reasonable.

 

What does the data represent, where does it come from, and what's the purpose of all this?

0 Kudos
Message 4 of 9
(2,634 Views)

Thanks.  This makes sense.  I will compare each element of the array inside a loop starting with the most significant digit and determine from there which array has the largest number.

0 Kudos
Message 5 of 9
(2,622 Views)

If the value of each element is guaranteed to be an integer in the range 0...15, it might be a hexadecimal single digits, making a loop-free (nothing wrong with loop, though) very simple version possible. 

 

All this assumes that the array is short. If it can be very long, more code is needed. What are the sizes?

(Sorry, posting by phone, no code example)

0 Kudos
Message 6 of 9
(2,596 Views)

The sizes for the first 3 positions can be from 0 to 9 and for the last 2 positions can be from 0 to 200. (both arrays can be up to 9.9.9.200.200).

0 Kudos
Message 7 of 9
(2,545 Views)

So, if first array is 1.1.1.10.5  and the second is 1.1.1.5.10    which is supposed to be bigger?

With what you described, the second since the first is 111105  and the second is 111510.

 

Is that correct?

 

And if the first is 1.1.1.10.5 and the second is 1.1.2.1.199, is the second bigger?  111105 vs. 1121199

0 Kudos
Message 8 of 9
(2,533 Views)

for the first 2 arrays: 1.1.1.10.5 will be larger than 1.1.1.5.10.  For the other 2 arrays you got it.  The second 1.1.2.1.199 will be larger.  Sorry if I didn't explain what I was trying to achieve a bit more clear.  I think I got the solution now.  I'm converting the string arrays to DBL then I'm comparing the arrays using the 'Compare Aggregates' option.  Seems to be working now.  Thanks for the support!

0 Kudos
Message 9 of 9
(2,528 Views)