1

Not sure how to ask my question, maybe the title doesn't reflect my question correctly, and if that is the case feel free to correct me.

I have two arrays

a = np.zeros((2, 2))
b = np.ones((3, 3))

I want a func that would yield the following result:

c = func(a, b)
print(c)

# array([[1., 1.],
#       [1., 1.]])

But if we switch position of a and b when we pass them to the function, we will get the following result:

c = func(b, a) # Note the change
print(c)

# array([[0., 0., 1.],
#       [0., 0., 1.],
#       [1., 1., 1.]])

Edit: for clarification, here is another example to answer @mercury's question.

a = np.zeros((2, 3))
b = np.ones((3, 2))

c = func(a, b)
print(c)

# array([[1., 1., 0.],
#       [1., 1., 0.]])
4
  • Some clarifications: what if a has shape (2,3) and b has shape (3,2) (Each array is bigger in a different dimension). Or are the arrays always nxn square arrays? Commented Aug 16, 2020 at 10:28
  • @Mercury I added another example to the post that should answer your question Commented Aug 16, 2020 at 10:31
  • So you want to keep the shape of the first array and replace all elements of it with the elements of the second array, if the second array has these elements? Commented Aug 16, 2020 at 11:02
  • @NiklasMertsch Yes, that's pretty much it. Commented Aug 16, 2020 at 11:13

1 Answer 1

1

I think I found an answer, not sure about it tho.

def func(a, b):
    x1, x2 = np.min((a.shape, b.shape), 0)
    c = a.copy()
    c[:x1, :x2] = b[:x1, :x2]
    return c

I'll be happy if someone who understands the question could reply if this actually solves it, and if so, how should this operation be called?

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

4 Comments

Looks good to me. If I understood your question correctly, something like this would be my answer.
@NiklasMertsch thank you for your answer, how would you call this operation btw? I thought something along the lines of fill_array but I'm not sure if it fully reflects the idea of the function.
This is your brainchild, name it however you want. If you solve some specific problem with it, maybe that problems gives you some inspiration. fill_existing(base, fill)?
@NiklasMertsch Thank you for your answer. If you are familiar with neural networks, I am using this function to connect the weights of two layers in a neural network while compensating for missing connections (for genetic a genetic algorithm).

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.