# A function that replaces any negatives with zeros

49 visualizzazioni (ultimi 30 giorni)
Eric Brown il 2 Apr 2022
Modificato: Stephen23 il 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.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Voss il 2 Apr 2022
Modificato: Voss il 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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Voss il 2 Apr 2022
Modificato: Voss il 2 Apr 2022
"How can the output be the same as the input?" Because there's no rule against it. You could make a function that just returns whatever input is given:
function x = return_input(x)
% that's it, nothing to do
end
I'm confused because you say, "I used what you wrote," but what you just posted doesn't look like what I posted. I posted two separate functions that will solve the problem, neither of which have an if statement, for instance.
Voss il 2 Apr 2022
Modificato: Voss il 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.

Accedi per commentare.

### Più risposte (2)

Geoff Hayes il 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).
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Stephen23 il 3 Apr 2022
Modificato: Stephen23 il 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?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by