1

I have two 3d numpy arrays a1 and a2 where len(a1) == len(a2) and a1[x, y, z] = id

I'm using this code to find out if there is changed data in any of the z layers

eq = a1 == a2
if eq.any():
    #find the indexes of the changed data

as the comment and title says I need to find the indexes of the changed data. basically I have a list of objects that correspond the the positions in the array and I need to update those objects based on the id pulled form the array. I want to do this as fast as possible as this list can get REALLY large possibly more than 120,000 entries. but only a hundred or so of these entries are likely to change at any one time. as such I'd like to obtain a list of the changed indexes so I can call the object at that index and update it.

I do need to maintain the three components of the index

is there a way to do this without looping through the list? perhaps with numpy.nonzero()

2 Answers 2

4

Two more options:

np.argwhere(a1!=a2)
np.where(a1!=a2)

They both do the same thing but produce results in different formats (one suitable for indexing the arrays, the other more readable)

Sign up to request clarification or add additional context in comments.

1 Comment

np.argwhere() appears to be what I'm looking for. thank you. it returns an array of arrays with the indexes that match the condition which is exactly what I needed.
1

I didn't understand the specifics of your question (e.g., the size/shape of your array); this solution should be general enough though. In any event, it's loop-less:

# two 2d arrays
x = NP.random.randint(0, 10, 20).reshape(5, 4)
y = NP.random.randint(0, 10, 20).reshape(5, 4)

ndx1, ndx2 = NP.nonzero((x - y))

# all indices in which the values of x & y differ, each index is a tuple
ndx = zip(ndx1, ndx2)  

# you can also use argwhere, returns each index as a 1d array vs. tuple:
NP.argwhere(dxy)

# finally, just generating an array the same size as the diff array with e.g.,
# '1' for each diff element and '0' for each non-diff:
dxy = x - y
NP.where(dxy=0, 0, 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.