belgarion
belgarion

Reputation: 115

If I have an array of positive and negative values, how do I only keep the positive ones, and replace the negative ones by 0?

I have a matrix A with a row of 100 values. When I do

B=A(A>=0);

My new matrix only has 50 values and I can't plot it anymore, because I need to specifically plot 100 values. How would I keep the placement of the empty values at 0?

Example:

 A= [1 -1 2 -2 3 -3]

B would have to be

B = [1 0 2 0 3 0]

Upvotes: 0

Views: 58

Answers (1)

Brendan Frick
Brendan Frick

Reputation: 1025

B = A;
B(A < 0) = 0;

A < 0 will return a a binary array [0 1 0 1 0 1] for your example. Calling B(A < 0) = 0 will set all positions of B with a 1 in A < 0 to 0.

Upvotes: 2

Related Questions