Merge two arrays with the same dimension based on a condition

I have two arrays with the same dimension:

a = [
[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 1, 1], ]

b = [
[0, 1, 1, 0],
[0, 0, 0, 0],
[2, 0, 0, 2],
[0, 0, 0, 0], ]

I would like to create a new one, only changing the values where B is not 0 and is different than A. The result would be:

c = [
[1, 1, 1, 1],
[1, 0, 0, 1],
[2, 0, 0, 2],
[1, 1, 1, 1], ]

How can I do this?

Answer

You can do assignment with boolean conditions:

a[b != 0] = b[b != 0]

a
array([[1, 1, 1, 1],
       [1, 0, 0, 1],
       [2, 0, 0, 2],
       [1, 1, 1, 1]])