Azzera filtri
Azzera filtri

Problem Using the Median Function with Complex Numbers

27 visualizzazioni (ultimi 30 giorni)
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.

Risposte (2)

Torsten
Torsten il 25 Lug 2024
Spostato: Torsten il 25 Lug 2024
c = a + (b-a)/2;
k = (sign(a) ~= sign(b)) | isinf(a) | isinf(b);
c(k) = (a(k)+b(k))/2;
But in every case, you set c = (a+b)/2, don't you ?
There is no update as c = (a-b)/2 in your code.
  1 Commento
Beste
Beste il 29 Lug 2024
Yes, actually the function meanof is included in the built-in matlab function median. The calculation
c = a +(b-a)/2 = (a+b)/2
and then the update
c(k) = (a(k)+b(k))/2
did not make sense to me since it doesnt change the numbers. But according to the answer of @Gkm4430, this is irrelevant and its purpose is to minimize inaccuracies due to floating point arithmetic, and to handle Infinite numbers.

Accedi per commentare.


Govind KM
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)
ans = 1x2
5 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
median(abs(a))
ans = 7.5000
median(a)
ans = -1.5000 + 6.0000i
abs(median(a))
ans = 6.1847
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
c = NaN + 2.5000i
%NaN occurs due to Inf-Inf
median([a,b])
ans = Inf + 2.5000i
%No NaN as "meanof" function checks for this case and calculates accordingly
I hope this clarifies the matter.
  2 Commenti
Beste
Beste il 29 Lug 2024
Modificato: Beste il 29 Lug 2024
Hello, thank you for the answer. Actually I understood the order of calculations makes difference, but the numbers in my example did not make sence to me mathematically. I add the captures to express myself better. My array is A, when I sort it with abs values, the two middle numbers are the numbers marked with red. Then, the absolute value of the median of A,where in this case the median is equal to the mean of these two, appears very different than the abs values of the red-marked numbers. This situation confuses me in the sense that what is the purpose of sorting the array elements according to the magnitudes at the first step.
Govind KM
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);

Accedi per commentare.

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by