How to calculate median of an array in FIJI/ImageJ?

124 Views Asked by At

I want to find the median value of a column in the Results table. I am pulling the column out as an array using :

Table.getColumn (Column);

So now I just need to know how to find the median value of this array. I know that:

Array.getStatistics(array, min, max, mean, stdDev)

can return other summary statistics, but it doesn't have median for some reason.

I am struggling to find any sort of median function. Does anyone have any idea?

Cross-posted from Reddit and Image.sc Forum

1

There are 1 best solutions below

0
quantixed On

The OP got an answer on image.sc which I am copying below for others searching for an answer to this question.

You can easily find the median of an array using the existing array functions. You can use: Array.sort(array) - Sorts array , which must contain all numbers or all strings. String sorts are case-insensitive. (Built-in Macro Functions) to organise your array. Here is some macro code for a function that returns the median value for an array.

array1=newArray(3,5,9,2,7);
array2=newArray(10,3,7,9);

print(median(array1));
print(median(array2));


function median(x){
    x=Array.sort(x);
    if (x.length%2>0.5) {
        m=x[floor(x.length/2)];
    }else{
        m=(x[x.length/2]+x[x.length/2-1])/2;
    };
    return m
}