I'm trying to move some old code I have from Matlab to Python and I'm having an issue with the way data is calculated by each. As a simple example, I have a stack of 2000 2D images called X. I want to calculate the mean of the stack and then subtract that from every image in the stack. In Python I have the following:
X = np.double(X)
mean = np.mean(X)
delta = np.zeros(X.shape)
for i in range(2000):
delta[:, :, i] = X[:, :, i] - mean
print(np.mean(delta), np.sum(delta), np.median(delta), np.std(delta, ddof=1))
The mean of delta is -5.900437149625761e-16, the sum is -1.2038071872666478e-08, the median is -0.18050000000005184, and the standard deviation is 31.64658195121922.
However, if I use the following approach in Matlab I get something different:
X = double(X);
delta = zeros(size(X));
m = mean(X, 3);
for i = 1:2000
delta(:, :, i) = X(:, :, i) -m;
end
Here, the mean is -7.478523241320775e-16, the sum is -1.525768311694264e-08, the median is -0.180500000000052, and the standard deviation is 31.646581951219204.
I am aware that these differences are very small but such differences can still have a slight impact in my results. Considering the simplicity of the calculation I'm surprised that they are different at all. What can be causing this to happen?