It's been a loooong time since I was in a math class and can't remember how to do this.
I need formula to reduce a range of values to a scale of 1 to 100.
For example my highest value is 900 and the lowest is 27.
So 900 would become 100 and 27 would become 1.
The numbers in between will be converted to a number between 1 and 100.
Oh, and this needs to work with fractions and negative numbers.
For example a possible range could be 0.53 to -0.67.
will work for you, and it doesn't give you an error when a zero is involved.
Thx again, but I did it this way.
Code:
if (vHigh - vLow) <> 0 then
X = (((vNewVal - vLow) / (vHigh - vLow)) * 99) + 1
else
'either all values are the same, or there's only one value left
x = 100
end if
This seems to work. I generated a thousand values to check it using a list box to display data and scaled values:
Code:
Dim MyData() As Single, MinValue As Single, MaxValue As Single
Const Pop = 1000, Range = 100
Private Sub Command1_Click()
ReDim MyData(Pop)
' Generate Sample Data and Establish Limits to Include Some Negative Values
For I = 1 To Pop
MyData(I) = Rnd * Range - Range / 2
If MyData(I) < MinValue Then MinValue = MyData(I)
If MyData(I) > MaxValue Then MaxValue = MyData(I)
Next
'Display Actual and Scaled Values
For I = 1 To Pop
List1.AddItem MyData(I) & Space$(3) & ((MyData(I) - MinValue) / (MaxValue - MinValue)) * (Range - 1) + 1
Next
End Sub
Kind of reminds me of my engineering days when we built nomographs.
Last edited by Code Doc; Sep 13th, 2008 at 07:51 PM.
That's pretty close to what Merri and I came up with.
I did spot a small prob with your code,
MinValue is going to start as = 0
So if your smallest value is greater than 0, MinValue won't be raise to match it.
Here's a way to fix it
Code:
For I = 1 To Pop
MyData(I) = Rnd * Range - Range / 2
If I = 1 then
MinValue = MyData(I)
MaxValue = MyData(I)
Else
If MyData(I) < MinValue Then
MinValue = MyData(I)
ElseIf MyData(I) > MaxValue Then
MaxValue = MyData(I)
End if
End If
Next
That's pretty close to what Merri and I came up with.
I did spot a small prob with your code,
MinValue is going to start as = 0
So if your smallest value is greater than 0, MinValue won't be raise to match it.
Here's a way to fix it
Code:
For I = 1 To Pop
MyData(I) = Rnd * Range - Range / 2
If I = 1 then
MinValue = MyData(I)
MaxValue = MyData(I)
Else
If MyData(I) < MinValue Then
MinValue = MyData(I)
ElseIf MyData(I) > MaxValue Then
MaxValue = MyData(I)
End if
End If
Next
P.S. I'll bite, what's a nomograph?
Hmmm... I added 1 to the expression clear to the right of the statement, so it should have started with 1.
Nomographs work just like this using rescaling of an existing scale. For example, Degrees Centigrade (0 to 100) can be derived for Degrees Fahrenheit( 32 to 212). The graphs usually are drawn parallel to each other. A pivot point is sometimes added so that a line going from one to the other has to pass through the pivot point to obtain corrresponding readings. When the line crosses the two nomographs and goes through the pivot point, readings where the lines cross the nomographs are the same on each.
In the case of the two temperature scales, I believe no pivot point would be used, but the connecting ruler would have to remain perpendicular to the two scales. I recall that nomograph "art" really becomes challenging when a pivot point is required and the two charts are nonlinear, such as logarithmic.
Last edited by Code Doc; Sep 14th, 2008 at 08:28 AM.
Sounds like you've done more interesting math than I have
But I'll stick with my guns on this code.
In fact, MaxValue had a similar problem, if your highest value was below zero, MaxValue would never
lower itself to match it.
I added some code and controls to demo the issue.
I also moved the variables inside the sub so they could reset each time the command button is clicked.
You can download the project at the bottom of this post, or try to recreate the form from the following code.
Code:
Option Explicit
Const Pop = 1000, Range = 100
Private Sub Command1_Click()
Dim MyData() As Single, MinValue As Single, MaxValue As Single
Dim MinValue2 As Single, MaxValue2 As Single
Dim I
ReDim MyData(Pop)
' Generate Sample Data and Establish Limits to Include Some Negative Values
For I = 1 To Pop
' MyData(I) = Rnd * Range - Range / 2
If optRange(0).Value Then
'all above 0
MyData(I) = (Range * Rnd) + 1
Else
'all below 0
MyData(I) = ((Range * Rnd) - 1) - 100
End If
'old code
If MyData(I) < MinValue Then MinValue = MyData(I)
If MyData(I) > MaxValue Then MaxValue = MyData(I)
'new code
If I = 1 Then
MinValue2 = MyData(I)
MaxValue2 = MyData(I)
ElseIf MyData(I) < MinValue2 Then
MinValue2 = MyData(I)
ElseIf MyData(I) > MaxValue2 Then
MaxValue2 = MyData(I)
End If
Next
'Display Actual and Scaled Values
List1.Clear
List2.Clear
For I = 1 To Pop
List1.AddItem MyData(I) & Space$(3) & ((MyData(I) - MinValue) / (MaxValue - MinValue)) * (Range - 1) + 1
List2.AddItem MyData(I) & Space$(3) & ((MyData(I) - MinValue2) / (MaxValue2 - MinValue2)) * (Range - 1) + 1
Next
'display the min and max values of the two systems
Text1(0).Text = CStr(MinValue)
Text1(1).Text = CStr(MaxValue)
Text1(2).Text = CStr(MinValue2)
Text1(3).Text = CStr(MaxValue2)
End Sub
basically, MinValue and MaxValue needed to be initialized.
I learned this one the hard way myself
Last edited by longwolf; Sep 14th, 2008 at 07:40 PM.
Reason: (needed to clear the lists on each click)