简体   繁体   中英

increase multiplier based on array index

My iq isnt high enough to comeup with a formula for this. Say i have array of 200 elements. i have a set multiplier with which im multiplying each array element in a for loop.

    public float[] myArray = new float[200];
    public float multiplier = 150;
    public float multiplierFactor = 2;

    public void enhance()
    {
        for(int i = 0; i < myArray.Length; i++)
        {
            if(i > myArray.Length * 0.75f)
            {
                //  ... what to do here to multiplierFactor ...
            }
            myArray[i] *= (multiplier * multiplierFactor);
        }
    }

What i would like to do is: calculate multiplierFactor when 'i' has reached certain point in array (eg (int)(i > myArray.Length * 0.75f) ) such that start increasing the multiplierFactor from its original value of 1, go up to specified highest value (eg multiplierFactor=2 so it goes from 1 to 2 or any specified value) then after max point is reached (eg 2) start decreasing the multiplier and go back to original value over the left over indexes of the array. So at the last element the multiplierFactor is again back to original value of 1. Kind of like a sine wave.

Okay, you can try this. It should work:

if(i>= myArray.Length*0.75 && i<=myArray.Length*(0.75+0.25*0.5))
{
    #positive slope line equation for multiplierFactor
    multiplierFactor=1+((i+1-myArray.Length*0.75)/(myArray.Length*0.25*0.5))
}
else if(i>myArray.Length*(0.75+0.25*0.5))
{
    #negative slope line equation for multiplierFactor
    multiplierFactor=2-((i+1-myArray.Length*0.75)/(myArray.Length*0.25*0.5))
}
else
{
    multiplierFactor=1
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM