3

I have two numpy arrays:

A  = [ 186.,  176., 158.,  180.,  186.,  168.,  168.,  164.,  178.,  170.,  189.,  195.,
       172.,  187., 180.,  186.,  185.,  168.,  179.,  178.,  183.,  179.,  170.,  175.]

B = [  -1., 60., 45., 58., 70., 70., 60., 64., 68., 78., 80., 78.,
       66., 74., 84., 85., 75., 60., 71., 67., 88., -1., 70., 60.]

I want to stack only Positive values and I don't know how?

I used numpy masked array to mask "-1"s from B

thank you

1
  • " stack only Positive values": What does that mean? If C is the result of the operation you want to do, could you add to your question exactly what C would be? Commented Nov 15, 2013 at 22:56

3 Answers 3

4

Assuming you want to stack a and b for every position where b>=0:

check = (b>=0)
c = np.vstack((a[check], b[check]))

should do the job...

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

Comments

0

Maybe use an unsigned value for it?

unsigned int array[2] = {0, 1};

http://en.wikipedia.org/wiki/Integer_(computer_science)#value_and_representation

Comments

0

If you only need to stack 1-dimensional arrays and the removal of negative numbers from B will make its shape the same as A, this should work:

np.vstack( (A, B[B >= 0.0]) )

2 Comments

this will must probably give you a ValueError since the arrays being stacked have different sizes...
@SaulloCastro Well it's unclear what the op wants, but yes I see what you mean. More likely he wants to filter out the elements in A and B if the element in B is negative, as your answer suggests.

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.