476

410

Is there an easier way to find the median value in a time series array? Currently I copy the array, sort the copy and then select the median. Seems longwinded but couldn't find any standard functions to achieve the same thing. Anything better?

Thanks

V

I understand what you are doing but I don't know HOW you do it.

Of course, I can research how to "copy the array" and how to "sort the copy".

But if you could explain how __you__ do it, it would make the task a bit easier.

Thank you, Helmut

1122

I understand what you are doing but I don't know HOW you do it.

Of course, I can research how to "copy the array" and how to "sort the copy".

But if you could explain how __you__ do it, it would make the task a bit easier.

Thank you, Helmut

It's pretty easy if you use the standard functions:

614

I understand what you are doing but I don't know HOW you do it.

Of course, I can research how to "copy the array" and how to "sort the copy".

But if you could explain how __you__ do it, it would make the task a bit easier.

Thank you, Helmut

the copy and sort is straight forward but on reflection, the selection of median isn't. What I have done by simply dividing array length by 2 is good enough for me because I have enough elements to not worry about it, but my method is technically incorrect. if my array length was 5, I need to select element 2 but if it was 6, I need to find the mean of elements 2 & 3. To accurately find the median,imho, is quite convoluted and my method is technically inaccurate. Hence, I hoped there might be a function to help... by the looks of it there isn't.... I might try and build it. Anyway, abridged code below is my current status...

Thanks for the responses

V

int len=50; double array[len],copy[len]; static int val; if (New_Bar) { val++; array[0]=val; ArrayCopy(copy,array,0,0,WHOLE_ARRAY); ArraySort(copy,WHOLE_ARRAY,0,MODE_DESCEND); median=copy[len/2]; for(i=len-1;i>=1;i--) { array[i]=array[i-1]; } }

1122

the copy and sort is straight forward but on reflection, the selection of median isn't. What I have done by simply dividing array length by 2 is good enough for me because I have enough elements to not worry about it, but my method is technically incorrect. if my array length was 5, I need to select element 2 but if it was 6, I need to find the mean of elements 2 & 3. To accurately find the median,imho, is quite convoluted and my method is technically inaccurate. Hence, I hoped there might be a function to help... by the looks of it there isn't.... I might try and build it. Anyway, abridged code below is my current status...

Thanks for the responses

V

614

For sake of completion, here is the function I wrote...

V

[edited as per following discussion]

double ArrayMedian(double array[]) { double median; double copy[]; int len=ArraySize(array); ArrayResize(copy,len); ArrayCopy(copy,array,0,0,WHOLE_ARRAY); ArraySort(copy,WHOLE_ARRAY,0,MODE_DESCEND); if (len%2==0) // it's even { median=(copy[len/2]+copy[(len/2)-1])/2.0; } else // it's odd { median=copy[len/2]; } return(median); }

1122

For sake of completion, here is the function I wrote...

V

median=(copy[len/2]+copy[(len/2)+1])/2; in this case median may result in a value not present in the copy array. It will be a mere median value. If that's your intention you

can simply use ArrayMinimum() and ArrayMaximum() and find the median like that: median = (max+min)/2 without sorting the array. But if you want the find the median element

in the array use simply median = copy[len/2];

803

median=(copy[len/2]+copy[(len/2)+1])/2; in this case median may result in a value not present in the copy array. It will be a mere median value. If that's your intention you

can simply use ArrayMinimum() and ArrayMaximum() and find the median like that: **median = (max+min)/2 without sorting the array.** But if you want the find the median element

in the array use simply median = copy[len/2];

The median is not (max+min)/2 except for the special case where the array has only 2 elements.

http://www.purplemath.com/modules/meanmode.htm

Median is explicitly described as the middle element in a rank-sorted series. If the series contains an even number of elements then the median is explicitly described as being the mean value of the middle two values.

1122

The median is not (max+min)/2 except for the special case where the array has only 2 elements.

http://www.purplemath.com/modules/meanmode.htm

Median is explicitly described as the middle element in a rank-sorted series. If the series contains an even number of elements then the median is explicitly described as being the mean value of the middle two values.

803

For sake of completion, here is the function I wrote...

V

V make sure the typecast is double (not integer) when you compute the mean which require you to divide by 2

median=(copy[len/2]+copy[(len/2)-1])/2.; //

Is there an easier way to find the median value in a time series array? Currently I copy the array, sort the copy and then select the median. Seems longwinded but couldn't find any standard functions to achieve the same thing. Anything better?

Thanks

V