2

I have two numpy arrays ym and r_div with shapes (110,) and (120,). I want to divide ym with r_div, i.e. the first element of ym gets divided by the first element of r_div, the second element by second element and so on. I tried converting both the arrays to lists and dividing them, but no avail. Tried a truediv and map, but not able to get the values in map.

This is what I tried so far:

from operator import truediv
norm_y = map(truediv, ym_list, first)

and converting them to lists, and dividing them, which resulted in a TypeError.

The arrays don't match in shapes, but I want to use only the first 110 elements of r_div.

1 Answer 1

2

You can slice r_div to get the first 110 values before you divide:

ym / r_div[:110]

The / operator does division element-wise on NumPy arrays.

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

4 Comments

"true division" usually refers to the non-floored version: i.e., the one that corresponds to / rather than // (in Python 3, at least).
Hi, thank you for your response @ajcr. I tried that. The resultant array is of shape (110, 110). I want it to be (110,). Basically, in // or /, every element in ym is getting divided by every element in r_div. the resultant array should have elements as first element = first of ym/first of r_div, second element = second of ym/second of r_div, and so on.
@magnus_prime: if your arrays ym and r_div are both one-dimensional, I don't see how the division will create a two-dimensional array - are you certain that both only have one dimension?
@ajcr : Sorry, got it! I made a typo while copying your code in the variable names! Thanks.

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.