A function that replaces any negatives with zeros
35 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Eric Brown
el 2 de Abr. de 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 comentarios
Respuesta aceptada
Voss
el 2 de Abr. de 2022
Editada: Voss
el 2 de Abr. de 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 comentarios
Voss
el 2 de Abr. de 2022
Editada: Voss
el 2 de Abr. de 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
el 2 de Abr. de 2022
Editada: Voss
el 2 de Abr. de 2022
Let me illustrate that my answer works:
x = randn(5)
y = NoNegatives(x)
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.
Más respuestas (2)
Geoff Hayes
el 2 de Abr. de 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 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!