Problem Using the Median Function with Complex Numbers
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.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. 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. digital signal processing, embedded matlab function MATLAB Answers — New Questions