A function that replaces any negatives with zeros

3 views (last 30 days)
Eric Brown
Eric Brown on 2 Apr 2022
Edited: Stephen23 on 3 Apr 2022
function y = NoNegatives(x)
if x(x<0)
y = (x == 0);
else
y = x;
end
I am to create a function that takes a vector as the input, changes any negative values to zero, and then returns the vector as the output.
This is what i have but i keep getting logic values as an output and non-zeros give a false output. I'm not sure why because my condition uses x < 0.

Accepted Answer

Voss
Voss on 2 Apr 2022
Edited: Voss on 2 Apr 2022
function x = NoNegatives(x)
x(x < 0) = 0;
Note that this function changes x in-place, so the output is also x - no need for a second variable y, although you could do it that way too:
function y = NoNegatives(x)
y = x;
y(x < 0) = 0;
% y(y < 0) = 0; % same, since y = x
  3 Comments
Voss
Voss on 2 Apr 2022
Let me illustrate that my answer works:
x = randn(5)
x = 5×5
-0.2012 0.4490 1.6008 -0.0854 -0.2275 1.0000 -1.1090 0.1217 -1.9189 -1.5017 -0.1674 -0.1472 -1.7229 0.6469 -1.6964 0.5773 0.0950 0.5282 0.2454 -1.2044 -0.0490 1.1158 -0.5440 0.3823 1.2742
y = NoNegatives(x)
y = 5×5
0 0.4490 1.6008 0 0 1.0000 0 0.1217 0 0 0 0 0 0.6469 0 0.5773 0.0950 0.5282 0.2454 0 0 1.1158 0 0.3823 1.2742
function x = NoNegatives(x)
x(x < 0) = 0;
end
See @Geoff Hayes's answer and @Walter Roberson's answer for good explanations of what's going on, which are better than I could've explained it.

Sign in to comment.

More Answers (2)

Geoff Hayes
Geoff Hayes on 2 Apr 2022
@Eric Brown - when you do something like
x<0
then this will return a logical array of 0's and 1's, where a 0 indicates that the element at that index is greater than or equal to zero, and a 1 indicates that the element at that index is less than zero (as per your condition). Then
x(x<0)
will return a subset of x such that only those elements corresponding to the logical one is returned...which is what you are being asked to find. But note this is a subset of x and so is an array which may have zero or more elements. If empty, then this would mean that there are no elements of x that are less than zero. if one element, then there is just one element less than zero. If more than one element, then there are multiple elements less than zero. Regardless as to which scenario occurs, do any of them make sense as a condition to an if statement? Do you even need an if statement?
All you need to do is find those elements that are less than zero (which you are doing) and then set (hint: assign) them to 0. Note that the
x == 0
is the equality operator. Here you are saying elements of x that are identical to 0...not assigning to zero. And like with x<0 this returns a logical array of 0's and 1's...which means that
y = (x == 0);
is a logical array of 0's and 1's (which explains why you are seeing this).

Stephen23
Stephen23 on 3 Apr 2022
Edited: Stephen23 on 3 Apr 2022
Simpler and much more efficient than your approach using indexing:
function y = NoNegatives(x)
y = max(0,x);
end
Why use slower, more complex code?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!