Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Powershell Convert range of numbers into another list of numbers maintaining ratio

Recently I was working on some PowerShell GUI project, Where I wanted to convert a range of number into another range of number maintaining ratio, for this I had to use one Arithmetic Math formula and, for example I have numbers between 1 to 200, Which I wanted to fit into new number of range between 15 to 70

Using the Math calculations first number 1 will become 15 and last number 200 will become 70. The other mid numbers will be fit into the new range of numbers between 15 to 70.

Powershell maths linear algebra artificial intelligence deep learning.jpg

To convert array to fit into new array you will need to use below Math algorithm. You need minimum and maximum number of given arrays. Then you require Range number (difference between largest and smallest number). once you have all the values do the arithmetic calculation using below formula. 

Powershell math arithmetic formula algorithm convert old array to new array.png

I have simplified above formula into PowerShell as below.

Microsoft Powershell math arithmetic formula algorythm formula Array minimum maximum range mean mode median artificial intelligence.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#Old Numbers array min and max
$oldArrayMin = 1
$oldArrayMax = 200

#New Numbers array min and max
$newArrayMin = 15
$newArrayMax = 70

#Old and New range number
$oldRangeNumber = $oldArrayMax - $oldArrayMin
$newRangeNumber = $newArrayMax - $newArrayMin

#Take any number to test
$oldNumber = 150

#Formula
$newValue = ((($oldNumber - $oldArrayMin) * $newRangeNumber)/ $oldRangeNumber) + $newArrayMin
$newValue

#calculation on multiple numbers
foreach ($test in 1..200) 
{
    (($test - $oldArrayMin) * $newRangeNumber / $oldRangeNumber) + $newArrayMin
}

To make it more simple I have created below script, you can use it as below with parameters.

Convertto-NewArray microsoft powershell arithmetic math calculations formula algorythm deep learning artificial intelligence.png

Download Convertto-NewArray.ps1 script here or it is also available on github.com/kunaludapi.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
#requires -version 4
<#
.SYNOPSIS
    Convert range of numbers into another range of numbers maintaining ratio.
.DESCRIPTION
    Convert range of numbers into another range of numbers maintaining ratio using Maths arithmetic formula. 
.PARAMETER OldArray
    Prompts you for old numbers array list or range default value is from 1 to 50.
.PARAMETER NewArray
    Prompts you for New numbers array list or range default value is from 100 to 200.
.PARAMETER RoundNew
    This is a switch how do you want your new array list rounding or without rounding numbers
.INPUTS
    list of numbers in [int[]]
.OUTPUTS
    Output is on console directly with oldarray and newarray list.
.NOTES
  Version:        1.0
  Author:         Kunal Udapi
  Creation Date:  27 September 2020
  Purpose/Change: New script written for some other project where charts are involved for data science.
.EXAMPLE 1
    PS C:\>Convertto-NewArray.ps1

    This command convert number of range from 1 to 50 to 100 to 200.
.Example 2
    PS C:\>.\Convertto-NewArray.ps1 -OldArray 10, 20, 30, 40 -NewArray 1,4,7 -RoundNew

    Here how the number you can use, Withe RoundNew parameter result is rounded to whole integer.
.EXAMPLE 3
    PS C:\>.\Convertto-NewArray.ps1 -OldArray (1..10) -NewArray (55..60)

    This is another example you can take use number array information.
#>

[CmdletBinding(SupportsShouldProcess=$True,
    ConfirmImpact='Medium',
    HelpURI='http://vcloud-lab.com')]
Param
(
    [parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
    [alias('Old')]
    [int[]]$OldArray = 1..50,
    [parameter(Position=1, ValueFromPipelineByPropertyName=$true)]
    [alias('New')]
    [int[]]$NewArray = 100..200,
    [switch]$RoundNew
)

begin {
    $measureOld = $OldArray | Measure-Object -Minimum -Maximum
    $oldArrayMin = $measureOld.Minimum
    $oldArrayMax = $measureOld.Maximum

    $measureNew = $NewArray | Measure-Object -Minimum -Maximum
    $newArrayMin = $measureNew.Minimum
    $newArrayMax = $measureNew.Maximum

    $oldRangeNumber = $oldArrayMax - $oldArrayMin
    $newRangeNumber = $newArrayMax - $newArrayMin
}

process {
    if ($RoundNew.IsPresent -eq $false) {

            foreach ($number in $OldArray) 
            {
                if (($number -eq 0) -or ($number -eq $oldArrayMin))
                {
                    [pscustomobject]@{
                        OldValue = $number
                        NewValue = $newArrayMin
                    }
                }
                else
                {
                    [pscustomobject]@{
                        OldValue = $number
                        NewValue = (($number - $oldArrayMin) * $newRangeNumber / $oldRangeNumber) + $newArrayMin
                    }

                }
            }
    }
    else {
        foreach ($number in $OldArray) 
        {
            if (($number -eq 0) -or ($number -eq $oldArrayMin))
            {
                [pscustomobject]@{
                    OldValue = $number
                    NewValue = $newArrayMin
                }
            }
            else
            {
                [pscustomobject]@{
                    OldValue = $number
                    NewValue = (($number - $oldArrayMin) * $newRangeNumber / $oldRangeNumber) + $newArrayMin
                }

            }
        }
    }
}

end {
}

Useful Articles
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6

Go Back

Comment

Blog Search

Page Views

4123210

Follow me on Blogarama