A function that replaces any negatives with zeros
조회 수: 37 (최근 30일)
이전 댓글 표시
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
채택된 답변
Voss
2022년 4월 2일
편집: Voss
2022년 4월 2일
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
Voss
2022년 4월 2일
편집: Voss
2022년 4월 2일
"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
2022년 4월 2일
편집: Voss
2022년 4월 2일
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.
추가 답변 (2개)
Geoff Hayes
2022년 4월 2일
@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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!