How do you make a program read a function for a script and creating a function?
이전 댓글 표시

I am very new to matlab. So sorry if I am a noob at programming simple things like this. What I am trying to do is, insert a vector and if the vector is a negative number turn it into a 0 and keep the rest of the positive number the same. so for this code it should pop out [12 0 4 0] but all I get is 12.
댓글 수: 2
Kenny Dang
2017년 1월 28일
Jan
2017년 1월 28일
@Kenny: Please post the code as text in following questions. Screenshots do not allow to copy&paste. Thanks.
채택된 답변
추가 답변 (2개)
Image Analyst
2017년 1월 28일
Get rid of lines 2-13 and have this:
x(x<0) = 0;
댓글 수: 2
Kenny Dang
2017년 1월 28일
Image Analyst
2017년 1월 28일
And you forgot to tag it as homework. I've done that for you. Get rid of lines 3 and 4 and make line 5 like Jan has. You might also initialize y before the loop
y = x;
and change lines 6-11 to this
if y(i) < 0
y(i) = 0
end
You certainly don't want line 8 in there - why return from your function while you're looping????
Guillaume
2017년 1월 28일
Your code is very puzzling. You have for loop using i that does nothing:
for i = 1:j
end %there's nothing in the body of the loop!
At the end of this loop, i is equal to j (what meaningless variable names!). Then you have an n loop that goes from 1 to the value of x(i), which has established is x(j), and thus -5. Hence that loop will not even execute. Even if it did start, there's nothing in the loop that depends on n.
I really don't understand what you were trying to do with these loop. In any case, no loop is needed:
x = [12 -3 4 -5];
y = x;
y(y<0) = 0
카테고리
도움말 센터 및 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!