Why do I receive different results when applying MEDIAN or NANMEDIAN to my matrix, vs. a single column, in MATLAB 7.13 (R2011b)?

3 views (last 30 days)
I have the following data:
A = [...
1+1j 6 ; ...
3-1j -4 ; ...
5-1j 2 ]
c = A(:,2)
When I apply MEDIAN to the matrix 'A', I receive -4 for the second column. However, when I apply MEDIAN to the column 'c', I receive 2. This also occurs with NANMEDIAN.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 16 Feb 2012
The main difference between the two data arrays is that the matrix 'A' is complex, whereas the column 'c' is purely real.
MEDIAN and NANMEDIAN both sort the values in the input data. When the data class is complex, the sort operation uses the absolute value of the data. In that case, the comparison of negative and positive real numbers will depend on whether the absolute values of the negative numbers are larger than the absolute values of the positive ones. This is the cause of the discrepancy you are seeing.
You can try either of the following options:
1. Segment the data into two parts with each only containing real or complex entries using ISREAL. Then apply your median finding operation to each part separately, to obtain the corresponding results.
2. Create your own version of these functions that can handle a mixture of real and complex numbers by modifying the original functions (after saving backup copies of the original functions).

More Answers (0)

Products


Release

R2011b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!