3

I need to compare each element in a 3d array a to a 2d array b and change values in a if they exceed values in b.

a.shape = 4000,5,5
b.shape = 5,5

This gives me the correct result when comparing all elements in a to b:

a[a[:,:,:]>=b]=1
a[a[:,:,:]<b]=0

However, I need to perform this as part of a loop and on slices of a. In the loop I will pass (start:end) values for example:

a[a[10:25,:,:]>=b]=1
a[a[10:25,:,:]<b]=0

This raises the error:

     VisibleDeprecationWarning: boolean index did not match indexed
     array along dimension 0; dimension is 4000 but corresponding
     boolean dimension is 15  if __name__ == '__main__':

The result is correct for what I need but is there, or what is, the way to do this without raising an error?

Thank you

Kshitij Saraogi
  • 6,821
  • 8
  • 41
  • 71
BenP
  • 825
  • 1
  • 10
  • 30
  • what happens if you introduce a new variable like c = a[10:25,:,:]? – Glostas Jan 13 '17 at 09:58
  • 1
    `DepracationWarning` is from `numpy` and the question is still valid wrt. what BenP should do to be in compliance with numpy api; which is to `a` and "index" array sizes should match. – Dima Tisnek Jan 13 '17 at 10:00
  • The problem is this: `a.shape == (4, 2, 2) and b.shape == (2, 2)`, then `(a>=b).shape == (4, 2, 2)` and you can use it as index, but `(a[0:1, ]>=b).shape == (1, 2, 2)` and you can't use it as index directly. – Dima Tisnek Jan 13 '17 at 10:10
  • You can hack it via: `a[0:2, ][a[0:2, ] >= b] = 1` however I think there's a more elegant solution possible. For example if in-place mutation is not a hard requirement, you can build `a` from slices computed directly. E.g. `(a[0:2,] >= b) * 42. + (a[:2, ] < b) * 13.` – Dima Tisnek Jan 13 '17 at 10:14

0 Answers0