Quote:
Originally Posted by primajaya
Anybody can help me how to create rank function
for example I have some double value's like theese:
a=3.0; b=4.0; c=5.0; d=2.0; e=1.0; f=6.0; g=0.0;
from higher to lower value (6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0)
or the value of (f,c,b,a,d,e,g)
So the rank's should be like this
rank number: (th)
f=1 ; c=2; b=3; a=4; d=5; e=6; g=7;
What I need is a function that return the rank if I input the value
int rank (double value)
{
process all value's;
return (the rank of a value from all value's)
}
Pj..
|
This is a useful function to have around. I always intended to write a generic function for this, so here goes.
I am assuming you have an array with some values, but you want to keep the original array in the same order. I have created a function
RankArrray() that will return an array that points to the original array in ranked order. Default is descending order, ascending is option.
Since I choose not to change the original array, I return the result in an Array "Rank[], each value of which is the index of the value in the Input Array in ranked order.
As written the original input array must be single dimension, if you have an array with more than one dimension you need to modify the function or copy to a single dimension array first.
I include an example of how to test it and how to use the RankArray to point to the original in rank order. Drop the example in your expert folder, complile and attach to a chart - examine the experts log.
Try it out and see how it works.
//+-------------------------------------------------------------
void RankArray(double InputArray[], int &Ranked[], bool sort_descending=true) {
//Purpose: Input: an array with a set of numbers that have been calculated in some fashion
// Output is array Ranked[] such that
// Ranked[0] points to the first member of the InputArray[]
// i.e. InputArray[Ranked[0]] is highest value
// InputArray[Ranked[1]] is next highest, ect.
// InputArray[] is unchanged
// sort_descending optional, default value true - is for Descending Order
// false will sort in ascending order
// User can modify as necessary for two dimensional Arrays.
int InputArraySize=ArrayRange(InputArray,0);
// Create Temporary array to sort - with pointers to original element
double TempArray[1][2];
ArrayResize(TempArray,InputArraySize);
for (int i=0;i<InputArraySize;i++)
{TempArray[i][0]=InputArray[i];
TempArray[i][1]=i;
}
int sort_dir;
//Validate input values
if (!sort_descending) sort_dir=MODE_ASCEND;
else sort_dir=MODE_DESCEND;
//Sort the Array-first element only
ArraySort(TempArray,WHOLE_ARRAY,0,sort_dir);
//Create output array - ranked by pointing to orignal values in ranked order
ArrayResize(Ranked,InputArraySize);
for (i=0;i<InputArraySize;i++)
{Ranked[i]=TempArray[i][1];}
return;
}