I have a 10 x 10 array of values, A
. I need the median, M
, of all of those values. I can find the medians along the rows or along the columns easily:
M = median(A,1) %or
M = median(A,2)
However, M = median(A)
also returns the medians along the rows.
How can I find a single median of ALL the values? I know I could convert the array to one very very long vector, but that seems unpleasant and inefficient. Is there a simpler solution? I would like to be able to do this for multi-dimensional arrays as well.
Thanks!
First linearize by indexing with (:)
. This transforms any array into a column array. Then compute the median:
M = median(A(:));
I don't think that indexing with (:)
needs any memory reallocation. It just reads the array in column-major order.