Initializing the empty numpy array
y=np.empty((2,2),dtype=np.matrix)
b=np.empty((2,2),dtype=np.matrix)
Assigning values to above arrays
b[0][0]=np.mat([
[67,57],
[19,56]])
b[0][1]=np.mat([
[7,58],
[9,46]])
b[1][0]=np.mat([
[77,47],
[34,34]])
b[1][1]=np.mat([
[2,66],
[78,45]])
y[0][0]=np.mat([
[67,57],
[19,56]])
y[0][1]=np.mat([
[7,58],
[9,46]])
y[1][0]=np.mat([
[77,47],
[34,34]])
y[1][1]=np.mat([
[2,66],
[78,45]])
Printing the array
print(y)
print(b)
The y and b arrays are equal and it should print True but instead it is printing False
print(np.array_equal(y,b))
print(y==b)
Firstly, as Michael Szczesny mentioned, np.mat
is deprecated and you should avoid it. Object arrays are also kinda iffy but can serve a purpose sometimes. Although to answer the actual question:
To check for equality between these arrays of arrays you need to check that the inner arrays are equal and then go from there. To do this you can trick numpy into applying np.array_equal
element wise and then check with np.all
import numpy as np
deep_equal = np.vectorize(np.array_equal)
def inner_array_equal(a,b): return np.all(deep_equal(a,b))
a = np.empty(2,dtype=object)
b = np.empty(2,dtype=object)
a[0] = np.array([1,2])
a[1] = np.array([3,4])
b[0] = np.array([1,2])
b[1] = np.array([3,4])
print(a,b)
print(inner_array_equal(a,b))
Notice that I switched your code to be explicit object arrays (although dtype=np.matrix
will do the same thing) and filled them with np.array
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.