Hi there everyone! This is my first ever post in the Julia community and since I’m unfamiliar with the various tags and categories on here, I just put this question in General.
I’m currently in the process of porting a PIV Matlab script over to Julia for a professor at my university. However, neither Julia nor Matlab are in my CS curriculum so it’s slow going finding the ins and outs of both languages!
The Issue I’m Having
The following code is part of a local median filter being iteratively used on small windows of a larger matrix composed of complex numbers and NaN values. I’ve created this little function which just filters out the NaN’s, then takes the median of the real and imaginary parts separately, and recombines them for the return value.
Julia code
using Statistics
function im_median(collection)
i = filter(x -> !isnan(x), collection)
if length(i) > 0
real_part = median(real(i))
im_part = median(imag(i))
return real_part + im_part * im
end
# If all NaN
return NaN
end
For your reference, here is the equivalent Matlab code I’m translating. Also, full transparency since this is a port, this function was created as part of the MatPIV package by John Peter Acklam:
function y = mnanmedian(x)
i = ~isnan(x);
if any(i)
y = median(x(i));
else
y = NaN;
end
end
I defined this matrix to try to pinpoint the differing outputs. A 3x3 matrix mimics the size of the filter window, basically a point in the grid and all of its neighbors:
nan_matrix = [
0.5488135 + 0.71518937im NaN 0.42365480 + 0.64589411im;
0.43758721 + 0.89177300im 0.96366276 + 0.38344152im NaN;
0.56804456 + 0.92559664im NaN 0.02021840 + 0.83261985im
]
Which yields the following output:
# Julia output:
0.493200355 + 0.77390461im
# Matlab output:
0.4932 + 0.8035i
As you can see, the output for the imaginary end of things differs by enough to cause problems for the PIV algorithm down the road.
Is there something I’m missing with my own implementation of the complex median function?