Problem Using the Median Function with Complex Numbers
27 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I encounter an issue with the median function when using on complex numbers. I am using MATLAB 2022a. In the link: https://www.mathworks.com/matlabcentral/answers/96926-why-do-i-receive-different-results-when-applying-median-or-nanmedian-to-my-matrix-vs-a-single-colu#answer_106277 it is stated that the median function sorts the complex numbers according to their magnitudes. However, I tried the median function with the abs values of the complex numbers after seeing that the numbers are not like what I expected. In my case, computing the median then the magnitude and the reverse (computing the absolute values of an array then the median) did not produce the same result where it should have produced. With further debug in the built-in function median, I observed that this case occurs when the length of the array is an even number. I think that the problem is due to the function that calculates the mean (inside the median) for the two middle numbers. The function is:
function c = meanof(a,b)
% MEANOF the mean of A and B with B > A
% MEANOF calculates the mean of A and B. It uses different formula
% in order to avoid overflow in floating point arithmetic.
if islogical(a)
c = a | b;
else
if isinteger(a)
% Swap integers such that ABS(B) > ABS(A), for correct rounding
ind = b < 0;
temp = a(ind);
a(ind) = b(ind);
b(ind) = temp;
end
c = a + (b-a)/2;
k = (sign(a) ~= sign(b)) | isinf(a) | isinf(b);
c(k) = (a(k)+b(k))/2;
end
In the bold part, c is calculated as (a+b)/2. Then, according to k, it should be updated as c(k) = (a(k)-b(k))/2 to correct the calculation if the numbers have different signs whereas it remains the same since it is given as c(k) = (a(k)+b(k))/2.
Thank you in advance.
0 Commenti
Risposte (2)
Govind KM
il 25 Lug 2024
Hi Beste,
It seems to me that you believe the magnitude of the median of an array of complex numbers should be the same as the median of the magnitudes of the complex numbers.
In the case of complex numbers, the median function sorts them according to magnitudes and then finds the median. However, in the case of even array length, the mean of the complex numbers with the two middle magnitudes is returned, which does not necessarily have a magnitude equal to the average of the two middle magnitudes.
An example to illustrate this is:
a=[3+4i,-6+8i];
abs(a)
median(abs(a))
median(a)
abs(median(a))
The part of the code you referenced in the “meanof” function is designed minimize inaccuracies due to floating point arithmetic, and to handle Infinite numbers.
>>k=(sign(a)!=sign(b)) | isinf(a) | isinf(b);
This line of code identifies indices where a and b have different signs, or where they are Infinite, to calculate mean for these cases separately to ensure accuracy. An example where this is the case is given:
a=Inf+3i;
b=5+2i;
c=a+(b-a)/2
%NaN occurs due to Inf-Inf
median([a,b])
%No NaN as "meanof" function checks for this case and calculates accordingly
I hope this clarifies the matter.
2 Commenti
Govind KM
il 31 Lug 2024
I think the values of 'A' displayed here are rounded off. Could you share the output of this code?
format long;
disp(A);
Vedere anche
Categorie
Scopri di più su Multidimensional Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!