I am writing code for a function but I get an error message
이전 댓글 표시
I have to write a function (g) that satisfies several conditions.
I have tried to do this before, but never successfully.
This is what I have so far:
function mayday=g(x)
if x<-pi
mayday=-1;
elseif (x>=-pi)&&(x<=pi)
mayday=cos(x);
else mayday=sin(x)+cos(x);
end
When I call the function with individual x's, it works fine, but when define x as a vector
g(-2*pi:pi/4:2*pi)
I get the error message
Operands to the || and && operators must be convertible to logical scalar values.
Error in g (line 7)
elseif (x>=-pi)&&(x<=pi)
What am i doing wrong?
thanks for the help
채택된 답변
추가 답변 (2개)
Walter Roberson
2012년 5월 4일
0 개 추천
Please read about logical indexing.
Peyman
2012년 8월 9일
0 개 추천
NOT RELATED TO THE QUESTION: how did you right your question so clear! I used this help and it doesn't work for me!
댓글 수: 1
Walter Roberson
2012년 8월 9일
I am not certain what you mean but try http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!