I am writing code for a function but I get an error message
3 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Kye Taylor
2012 年 5 月 4 日
Just replace && with & and double pipes | | with | to operate on nonscalars.
2 件のコメント
Walter Roberson
2012 年 5 月 4 日
That will eliminate the error message, but it will not eliminate the problem of trying to apply an "if" statement to a vector expression.
Kye Taylor
2012 年 5 月 4 日
An if statement can be applied to a vector expression. It is handled by matlab as
if(vector) = if(all(vector))
which may be the desired behavior.. probably not, but that's a different question.
その他の回答 (2 件)
Peyman
2012 年 8 月 9 日
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
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!