If you want the absolute element-wise difference between both matrices, you can easily subtract them with NumPy and use numpy.absolute on the resulting matrix.
import numpy as np
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = np.absolute(np.array(X) - np.array(Y))
Outputs:
[[7 1 2]
[2 2 3]
[3 3 0]]
Alternatively (although unnecessary), if you were required to do so in native Python you could zip the dimensions together in a nested list comprehension.
result = [[abs(a-b) for a, b in zip(xrow, yrow)]
for xrow, yrow in zip(X,Y)]
Outputs:
[[7, 1, 2], [2, 2, 3], [3, 3, 0]]
Answer from miradulo on Stack OverflowIf you want the absolute element-wise difference between both matrices, you can easily subtract them with NumPy and use numpy.absolute on the resulting matrix.
import numpy as np
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = np.absolute(np.array(X) - np.array(Y))
Outputs:
[[7 1 2]
[2 2 3]
[3 3 0]]
Alternatively (although unnecessary), if you were required to do so in native Python you could zip the dimensions together in a nested list comprehension.
result = [[abs(a-b) for a, b in zip(xrow, yrow)]
for xrow, yrow in zip(X,Y)]
Outputs:
[[7, 1, 2], [2, 2, 3], [3, 3, 0]]
Doing this becomes trivial if you cast your 2D arrays to numpy arrays:
import numpy as np
X = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
Y = [[5, 8, 1],
[6, 7, 3],
[4, 5, 9]]
X, Y = map(np.array, (X, Y))
result = X - Y
Numpy is designed to work easily and efficiently with matrices.
Also, you spoke about subtracting matrices, but you also seemed to want to square the individual elements and then take the square root on the result. This is also easy with numpy:
result = np.sqrt((A ** 2) - (B ** 2))
It's likely because there a built-in functions with the same name, abs. The same is true for np.amax, np.amin and np.round_.
The aliases for the NumPy functions abs, min, max and round are only defined in the top-level package.
So np.abs and np.absolute are completely identical. It doesn't matter which one you use.
There are several advantages to the short names: They are shorter and they are known to Python programmers because the names are identical to the built-in Python functions. So end-users have it easier (less to type, less to remember).
But there are reasons to have different names too: NumPy (or more generally 3rd party packages) sometimes need the Python functions abs, min, etc. So inside the package they define functions with a different name so you can still access the Python functions - and just in the top-level of the package you expose the "shortcuts". Note: Different names are not the only available option in that case: One could work around that with the Python module builtins to access the built-in functions if one shadowed a built-in name.
It might also be the case (but that's pure speculation on my part) that they originally only included the long-named functions absolute (and so on) and only added the short aliases later. Being a large and well-used library the NumPy developers don't remove or deprecate stuff lightly. So they may just keep the long names around because it could break old code/scripts if they would remove them.
There also is Python's built-in abs(), but really all those functions are doing the same thing. They're even exactly equally fast! (This is not the case for other functions, like max().)

Code to reproduce the plot:
import numpy as np
import perfplot
def np_absolute(x):
return np.absolute(x)
def np_abs(x):
return np.abs(x)
def builtin_abs(x):
return abs(x)
b = perfplot.bench(
setup=np.random.rand,
kernels=[np_abs, np_absolute, builtin_abs],
n_range=[2 ** k for k in range(25)],
xlabel="len(data)",
)
b.save("out.png")
b.show()
The answer by Derek Roberts with numpy.minimum is almost correct. However, since the input values can be greater than 100, they should first be rescaled to 0-100 with %100 (mod). I'm adding an extra pair of values to demonstrate this:
a = np.array([101,105,90,102,90,10,50,1001])
b = np.array([99,110,85,110,85,90,60,2])
x = abs(a%100-b%100)
np.minimum(x, 100-x)
Generic computation:
M = 100
x = abs(a%M-b%M)
np.minimum(x, M-x)
Output:
array([ 2, 5, 5, 8, 5, 20, 10, 1])
This is a classic example of what Modulus Arithmetic is used for Let us consider an example
Morris provides everything you need to know about modulus here
My source: maths
"Definition Let m > 0 be a positive integer called the modulus. We say that two integers a and b are congruent modulo m if b − a is divisible by m. In other words, a ≡ b(modm) ⇐⇒ a − b = m · k for some integers "
Let us take 10 and 90 for example:
In other words, moving directly from 10 to 90 gives a difference of 80, but if you consider the wrap-around (going backward from 10 to 0 and then forward from 100 to 90), you get a smaller difference of 20.
I have provide a code which helps
For example:
import numpy as np
a = np.array([101, 105, 90, 102, 90, 10, 50])
b = np.array([99, 110, 85, 110, 85, 90, 60])
# Start by computing absolute difference
absolute_of_two_nos = np.abs(a % 100 - b % 100)
# Then consider the difference- wrap-around using modulus of 100
diff_mod_using_100 = 100 - np.abs(a % 100 - b % 100)
# Take the minimum of the direct diff and the modulus diff
result = np.minimum(absolute_of_two_nos, diff_mod_using_100)
print(result)
If I correctly understand what your definition is here, you can just use broadcasting.
np.mean(np.abs(X[:, None] - Y))
Take the difference, then abs, then mean:
np.mean(np.abs(X - Y))
Alternatively:
diff = X - Y
abs_diff = np.abs(diff)
mean_diff = np.sum(abs_diff) / (X.size * Y.size)