Invalid use of operator

97 次查看(过去 30 天)
Sarah K
Sarah K 2019-10-22
评论: Nikoleta 2023-10-22
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
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
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
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
CamboSlice 2019-12-17
I've made this mistake MANY times haha. @John D'Errico answered correctly, albeit slightly harsh.

请先登录,再进行评论。


Nikoleta
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
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
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 CenterFile 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!

Translated by