1

I need something like this

def qqq(i,j):
    if i+j>2:
        return 0.5
    else:
        return 0
n=3
dcdt=np.fromfunction(lambda i,j: qqq(i,j)*i*j, (n,n), dtype=int)

but with more complicated qqq. But it leads to the error "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". I know the problem is that function is called once. How can I do such array creation with the "if-elif-else" structure in the function?

1 Answer 1

2

You should turn your qqq function into something like:

def qqq(i, j):
    return np.where(i + j > 2, 0.5, 0)

See np.where's docs for details.

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

Comments

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.