09-01-2015 06:56 PM
hi,
this might be an easy one...
I have a array where the values range from 0,8 to 1
I need to scale this so 0,8 -> 0 and 1->255
any ideas?
09-01-2015 07:35 PM
Many of LabVIEWs basic math functions are polymorphic which means that you can take your array and subtract a scalar 0.8 then multiply by a scalar 255.
This should scale all values linearly.
09-01-2015 08:15 PM
It sounds like he wants integer. He likely wants to use a round to nearest as well.
09-01-2015 08:19 PM
09-01-2015 08:57 PM
Start by subtracting 0.8. That brings you down to a range of 0-0.2. This doesn't quite get you where you want to be. Multiply by 1275. In that case, 0.2*1275=255. That gets you to the range of 0-255. Use the rounding to bring yourself from decimal to integer.
If you feed a scalar value into subtract/multiply and the other input is an array, the scalar is subtacted/multiplied with respect to each element in the array.
09-01-2015 10:52 PM
Here's a simple method to scale an array input to a range.
Cheers
--------, Unofficial Forum Rules and Guidelines ,--------
'--- >The shortest distance between two nodes is a straight wire> ---'
09-01-2015 11:06 PM
Be careful with "simple." That solution is very easy to change the max/min range for your output. If your max/min values don't appear in your input array, you won't get the behavior you want. If the max and min are guaranteed to be in the array (or if you want the output to vary based on the input), this is a very easy method to get the output you desire. If you always want 0.8 to be 0 and 1 to be 255, you'll want to ensure those values WILL appear in your input or you'll want to do something that's more constant and less calculated on the fly.
09-02-2015 07:45 AM
@natasftw wrote:
Start by subtracting 0.8. That brings you down to a range of 0-0.2. This doesn't quite get you where you want to be. Multiply by 1275. In that case, 0.2*1275=255. That gets you to the range of 0-255. Use the rounding to bring yourself from decimal to integer.
If you feed a scalar value into subtract/multiply and the other input is an array, the scalar is subtacted/multiplied with respect to each element in the array.
Derp, this is what I get for not checking my math.
09-04-2015 01:02 PM - edited 09-04-2015 01:04 PM
So there is already a VI in vi.lib that does this for you. You give it an old Min and Max, a new Min and Max, an value that was in your old range, and it will output that ratio'd value in the new range.
vi.lib\Utility\Range And Ratio.llb\Scale Value to New Range.vi
Very handy been in my reuse library for a while. Obviously this only works with a mapped range and for a table would need some modifying.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
16 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
09-04-2015 04:33 PM - edited 09-04-2015 04:35 PM
I didn't know that existed in VI.lib.
I've been using my own subVI for years that does the same thing.
There is something I don't like about the arrangement of the terminals on the connector panel of the vi.lib version.
To me the Maximums should be to the top. The minimums to the bottom. The original range should be to the left next to the input value. The new range should be on the top and bottom further to the left closer to the output value to reflect the output value. That prefers a left to right flow, and keeping max values high on the icon, and min values low on the icon.
I do like their rescale glyphs.
Here is the context help for mine.