Python: converting iterated assignment with atomic assignment using numpy does not work when matrix height> 256
I am working using numpy 1.6.2 and python 2.7 .
Given a matrix
N x M x D
and a matrix
that contains a list of indices.
I have to fill the matrix of zeros with the
sum of the element
according to the definitions found in
Here is my code:
ACopy = zeros(A.shape) for j in xrange(0, size(A, 0)): i = I[j] ACopy[j, i, :] = A[j, i, :] + A[j, i + 1, :]
I = array([2, 0, 3, 2, 1])
A = array([[[ 0, 1, 2], [ ], [ ], [ ], [ ]], [ ], [ ], [ ], [ ], [ ]], [ ], [ ], [ ], [ ], [ ]], [ ], [ ], [ ], [ ], [ ]], [ ], [ ], [ ], [ ], [ ]]])
I'm trying to improve my code by avoiding the for loop in this way:
r = r_[0:len(I)] ACopy[r, I, :] = A[r, I, :] + A[r, I + 1, :]
I noticed that the output matrices are
different and I cannot figure out why. Any idea?
EDIT . I am calculating a lot of matrices and I am trying to use
is the output of the first method and
is the output of the second method. Sometimes the matrices are the same, but not every time. The output of the two methods should be the same or is there any burgundy case?
EDIT2 . I noticed that this weird behavior only happens when the height is over 256 . Here's my test suite:
from numpy import * w = 5 h = 257 for i in xrange(1000): Z = random.rand(w, h, 5) I = (random.rand(w) * h - 1).astype(uint8) r = r_[0:w] ZCopy = zeros(Z.shape) ZCopy2 = zeros(Z.shape) for j in xrange(0, size(Z, 0)): i = I[j] i, :] = Z[j, i, :] + Z[j, i + 1, :] I, :] = Z[r, I, :] + Z[r, I + 1, :] if (ZCopy - ZCopy2).any() > 0: ZCopy2, I) raise ValueError
source to share