Invalid use of operator
97 ビュー (過去 30 日間)
古いコメントを表示
Matlab does not like the < in my function. Why?
if profit >= 60 && <100 % if profit falls between $60 and $100
any ideas? thank you.
function [output] = display_profit(dollars)
if profit >= 60 && <100 % if profit is between $60 and $100
end
Invalid use of operator (it directs me to the < which it has an issue with)
2 件のコメント
Nikoleta
2023 年 10 月 22 日
I'm trying to raise to the power of two but it doesn't let me use .^2 or ^2. It says invalid use of operator.
Image Analyst
2023 年 10 月 22 日
@Nikoleta start your own question in a new thread, and include your line of code. It could be your variable is not a numerical variable. If you don't show us your line of code and tell us what variable type then only Walter will be able to help you because only he has the Crystal Ball Toolbox ;-)
回答 (2 件)
John D'Errico
2019 年 10 月 23 日
編集済み: John D'Errico
2019 年 10 月 23 日
That you know what your made up syntax means is great. But MATLAB cannot read your mind (yet, the mind reading toolbox has not been released.) Yes, you know what you intend there. :)
You wrote
if profit >= 60 && <100
And somehow you think that MATLAB should know you intend it to be shorthard for this:
if profit >= 60 && profit < 100
But given your shorthand try, why would it not be equivalent to this one instead?
if profit >= 60 && 60 < 100
Computers are literal things. Don't use shorthand.
if profit >= 60 && profit < 100
1 件のコメント
CamboSlice
2019 年 12 月 17 日
I've made this mistake MANY times haha. @John D'Errico answered correctly, albeit slightly harsh.
Nikoleta
2023 年 10 月 22 日
I'm trying to run a code for pythagorean triplets. But when i raise a,b,c to the power of 2 it doesn't let me use ^2 or.^2 . My code looks something like this. I want to find all triplets that when i sum them i get 1000.
v=[a b c]
for a.^2 + b.^2 =c.^2 ;
a < b < c ;
if a + b + c==1000
disp (v)
end
2 件のコメント
Image Analyst
2023 年 10 月 22 日
編集済み: Image Analyst
2023 年 10 月 22 日
I'm not sure why you posted here when I asked you to start your own discussion. Anyway, there are so many things wrong with this that it's clear you haven't even take the basic on-ramp training.
I'm not sure why you want to square anything if you "want to find all triplets that when i sum them i get 1000". Why don't you just use 3 nested for loops?
counter = 0;
for a = 1 : 999
for b = 1 : 999
for c = 1 : 999
if (a + b + c) == 1000
counter = counter + 1;
fprintf('Combination #%d: %d + %d + %d = 1000\n', counter, a, b, c);
end
end
end
end
To learn other fundamental concepts, invest 2 hours of your time here:
Nikoleta
2023 年 10 月 22 日
Thank you. I'm just doing matlab for the first time so i don't really know what i'm doing, i'm just trying my best.But i might try that training, thank you for your help.
参考
カテゴリ
Help Center および File Exchange で Introduction to Installation and Licensing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!