Reputation: 3862
I have three numpy arrays, one the source array, one destination array and one mask array. I want to replace the values in the destination with the same values from the source only at the places where the mask is equal to one.
My naiive try was:
import numpy as np
destination = np.arange(9).reshape((3,3))
source = np.ones((3,3))
mask = np.zeros((3,3)).astype(np.uint8)
mask[1,1]=1
destination[mask] = source[mask]
which leads me to destination
being
[[1, 1, 1],
[1, 1, 1],
[6, 7, 8]]
whereas I expect it to be
[[0, 1, 2],
[3, 1, 5],
[6, 7, 8]].
I do get the correct result, when I do
destination[mask==1] = source[mask==1].
My question is: Why are these two commands not identical, or what does the first even do?
Upvotes: 0
Views: 1256
Reputation: 476
First you must check inside the matrices and which matrix gives you what you want.
mask
Output
[[0, 0, 0],
[0, 1, 0],
[0, 0, 0]]
but destination[mask == 1]
gives you a boolean matrix
mask == 1
Output
[[False, False, False],
[False, True, False],
[False, False, False]]
whereas:
destination[mask]
Output
[[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]
[[0, 1, 2],
[3, 4, 5],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]]
but using destination[mask == 1]
gives you a single value which is 4. It's the same for the source[mask == 1]
which gives you the single value 1.
and if you use destination[mask==1] = source[mask==1]
instead of destination[mask] = source[mask]
you will only change the value 4 in the destination matrix.
I hope my explanation is clear.
Edit:
I hope I understand your question correct:
The simple integer indexing structure x[[i]]
gives you the i'th row of the matrix.
So destination[0,1,2]
gives:
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]
and for an understandable example the input destination[1,2,0]
leads to
[[3, 4, 5],
[6, 7, 8],
[0, 1, 2]]
Upvotes: 1