Usually, you need to implement a sort algorithm then find the median as in the middle of the sorted array...
I found the easiest way of doing it is using STL (Standard Template Library).
Here is the method,
for example, you have a two dimensional array
double mydata[mb_width][mb_height];
and you like to find the median value of this array..
To use STL library, you first have to include the header file algorithm.h
#include "algorithm.h"
then define a vector mytheta:
vector
pass value from mydata to mytheta;
for (int i=0;i< mb_width;i++){
for (int j=0;j< mb_height;j++){
mytheta.push_back( mydata[i][j]);
}
}
Then you are free to use sort()
sort(mytheta.begin(), mytheta.end());
And here is the median:
median= *(mytheta.begin()+mytheta.size()/2);
Easy, hr?
No comments:
Post a Comment