median2D.c 323 Bytes
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
1 2 3 4
#include "fd.h"

float	median2d(float **mat, int ny, int nx){

5
	int	k;
Tilman Steinweg's avatar
Tilman Steinweg committed
6 7 8 9 10 11
	float		*t, med=0.0;
	
	t = vector(1,nx*ny);
	
	k = nx*ny;
	memmove(&t[1], &mat[1][1], k*sizeof(float));
12
	quicksort(t,1,k);
Tilman Steinweg's avatar
Tilman Steinweg committed
13 14 15 16 17 18 19

	if (k%2)	med = t[k/2+1];
	else		med = (t[k/2] + t[k/2+1]) / 2.0;
	
	free_vector(t,1,nx*ny);
	return med;
}