2

Say I have the following setup:

x=np.array(range(0,100))
y=x**0.5
z=x**2

How could I get all the values of y between 4 and 8, and create a new array that had a row for those y values, but also a row for the corresponding z values?

i.e. 25 in x has a y value of 5 which is between 4 and 8, I'd like this value, but I'd also like the z value of 625 in the row below.

The math equations are just an example, the forumals could be anything including bessel functions.

Thanks

2
  • what about the values where y is outside that limit? what is your purpose with this code? Commented Nov 6, 2012 at 17:25
  • Hey it would be great if you can be more specific here and add some context to the question as where and how this is required. Commented Nov 6, 2012 at 17:40

1 Answer 1

2
>>> np.vstack((y, z))[:,(y >= 4) & (y <= 8)]
array([[  4.00000000e+00,   4.12310563e+00,   4.24264069e+00,
          4.35889894e+00,   4.47213595e+00,   4.58257569e+00,
          4.69041576e+00,   4.79583152e+00,   4.89897949e+00,
          ...
          7.81024968e+00,   7.87400787e+00,   7.93725393e+00,
          8.00000000e+00],
       [  2.56000000e+02,   2.89000000e+02,   3.24000000e+02,
          3.61000000e+02,   4.00000000e+02,   4.41000000e+02,
          ...
          3.72100000e+03,   3.84400000e+03,   3.96900000e+03,
          4.09600000e+03]])

Here, vstack() creates a matrix with two rows, one containing y and one containing z. The [...] then eliminates those columns that don't satisfy the criterion.

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.