1

My program needs to find the product of all the terms in a vector save for one row i, which is determined by a for loop. The numpy.delete function will not accept i as an input, only a number. Is there a workaround for this?

Example:

for i in range(some_range):
    arr=[some vector]
    section=np.delete(arr,i,axis=0)
    return prod(section)

This returns a ValueError: invalid entry message. The code works fine when I replace the i with 1. Is there any way to perform this operation while retaining the for loop?

1
  • Is your indentation correct? The way you have it here with the return inside the loop, it will only run once (making the loop pointless). Commented Oct 23, 2013 at 7:56

1 Answer 1

1

No need to delete anything. Just multiply the two subproducts (the elements before i and the elements after i):

In [10]: import numpy as np

In [11]: arr = np.arange(1, 10)

In [12]: i = 3

In [13]: np.prod(arr[:i]) * np.prod(arr[i+1:])
Out[13]: 90720

Like with your original code, you need to make sure that i is a valid index (if you don't, you'd get the product of the entire array).

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.