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: 

sorting array number in different ranges

Solved!
Go to solution

Hello,

I want to assign one color to several ranges of number. I would like to know the best way to accomplish it.

I created one example.

In this example, I have one array with numbers and I assign one color to values:

<2.5 green

2.5 - 5 yellow

5-7.5 orange

7.5-10 red

 

It is only one example and I have more than 4 ranges in my application. I would like to know if it exists another way to sort the array than using several case structures...

 

Thanks for help!.

Regards.

 

0 Kudos
Message 1 of 15
(4,355 Views)

Greetings, have you thought of using the "RGB to coulour" VI, and multiply the input by a constant? That way it would dinamically assign a colour without having a ton of cases, you just need to find the right constant to have a good distinction 

0 Kudos
Message 2 of 15
(4,330 Views)
Solution
Accepted by topic author Fonsi

@Daikataro wrote:

That way it would dinamically assign a colour without having a ton of cases, you just need to find the right constant to have a good distinction 


(That assumes that the ranges are all of equal lenght and that the desired colors are in numerical order. Way too restrictive!)

 

I would use threshold array on an array of boundary values and index into an array of colors. Here's an example:

 

 

 

 

(Note that all values >10 will also be red. modify as needed if you want yet another color for >10 or similar)

 

Adjusting the colors or boundaries is as simple as changing the array diagram constants.

Download All
Message 3 of 15
(4,324 Views)

I just wrote exactly the same code as Altenbach, but it took me longer, of course.

 

However, one possible small improvement in Altenbach's code: you can make the last two elements of the array the same value, and then any input over that value will be assigned the last color in the array.

sort_ranges_color.png

0 Kudos
Message 4 of 15
(4,300 Views)

@nathand wrote:

I just wrote exactly the same code as Altenbach, but it took me longer, of course.

 

However, one possible small improvement in Altenbach's code: you can make the last two elements of the array the same value, 


Why would that be an improvement? The last element just needs to be equal or larger (e.g. 10, as in my example), the actual value (within reason) does not matter. It is actually confusing for code readability to have two identical values in there.

0 Kudos
Message 5 of 15
(4,285 Views)

Sorry, improvement was the wrong word, I should have said alternative. Personally I think it's clearer since then you don't wonder what's special about the value 10, but the results are exactly the same either way.

 

I did discover, however, that you cannot use +Inf.

0 Kudos
Message 6 of 15
(4,274 Views)

@nathand wrote:

I did discover, however, that you cannot use +Inf.


Not just Inf. Any huge number (e.g. 1e20) will cause the interpolated value to be so close to the lower index value that is is identical within DBL resolution and rounding to +Inf adds nothing.

0 Kudos
Message 7 of 15
(4,265 Views)

Would using Search Ordered Table.vi solved the Inf issue?

 

Using Ordered Table.png

 

Ben64

0 Kudos
Message 8 of 15
(4,251 Views)

altenbach escribió:

@Daikataro wrote:

That way it would dinamically assign a colour without having a ton of cases, you just need to find the right constant to have a good distinction 


(That assumes that the ranges are all of equal lenght and that the desired colors are in numerical order. Way too restrictive!)

 

I would use threshold array on an array of boundary values and index into an array of colors. Here's an example:

 

 

 

 

(Note that all values >10 will also be red. modify as needed if you want yet another color for >10 or similar)

 

Adjusting the colors or boundaries is as simple as changing the array diagram constants.


Hello guys!.

Thanks for all the responses.

Altenbach give the solution. I added another case if I add negative values in ranges. I put one solution, I think it is effcient solution.

But, what happen if the ranges are of the different lenght?

 

Instead use 2.5,5, 7.5 and 10. use 2, 5, 9, 10...

 

Regards.

0 Kudos
Message 9 of 15
(4,205 Views)

@Fonsi wrote:

But, what happen if the ranges are of the different lenght?

 

Instead use 2.5,5, 7.5 and 10. use 2, 5, 9, 10....


With that code, it does not matter.  It always rounds up.  So if the number is between the first and second number, you will have the second color.


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
0 Kudos
Message 10 of 15
(4,197 Views)