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)
Here's one approach that is significantly faster than V2: take img1-img2, and multiply by 1 or -1 depending on img1>img2. Here's how it is implemented:
Copydef differenceImageV6(img1, img2):
a = img1-img2
b = np.uint8(img1<img2) * 254 + 1
return a * b
A test harness for testing performance:
Copyimport numpy as np
img1=np.uint8(np.random.randint(0, 255, (480, 640)))
img2=np.uint8(np.random.randint(0, 255, (480, 640)))
def differenceImageV1(img1, img2):
diff=np.empty_like(img1)
h, w=img1.shape
for y in range(h):
for x in range(w):
if img1[y, x]<img2[y, x]: diff[y, x]=img2[y, x]-img1[y, x]
else: diff[y, x]=img1[y, x]-img2[y, x]
return(diff)
def differenceImageV2(img1, img2):
return(np.uint8(np.abs(np.int16(img1)-img2)))
def differenceImageV3(img1, img2): # fast - but wrong result
return(img1-img2)
def differenceImageV4(img1, img2):
return np.where(img1>img2, img1-img2, img2-img1)
def differenceImageV5(img1, img2):
a = img1-img2
b = img2-img1
c = img1>img2
return a*c + b*(~c)
def differenceImageV6(img1, img2):
a = img1-img2
b = np.uint8(img1<img2) * 254 + 1
return a * b
import timeit
def testit():
for fn in [differenceImageV2, differenceImageV3, differenceImageV4, differenceImageV5, differenceImageV6]:
print fn.__name__, np.sum(fn(img1, img2).astype('int64')),
print timeit.timeit("%s(img1, img2)" % fn.__name__, "from test import img1, img2, %s" % fn.__name__, number=1000)
if __name__ == '__main__':
testit()
and resulting performance numbers:
CopydifferenceImageV2 26071358 0.982538938522
differenceImageV3 39207702 0.0261280536652
differenceImageV4 26071358 1.36270809174
differenceImageV5 26071358 0.220561981201
differenceImageV6 26071358 0.154536962509
differenceImageV6 is about 6x slower than the incorrect differenceImageV3, but still about 6x faster than the previous best differenceImageV2. differenceImageV1 isn't tested because it's easily a few orders of magnitude slower than the rest.
Note: I included an np.where approach for comparison; I thought it might have good performance but it turns out to be fairly poor. It seems that performing slicing by a boolean array is quite slow in NumPy.
If you have opencv available, you can also use:
Copydef differenceImageV4(img1, img2):
return cv2.absdiff(img1, img2)
which is almost the same speed as differenceImageV3.