Invalid use of operator

193 views (last 30 days)
Sarah K
Sarah K on 22 Oct 2019
Commented: Nikoleta on 22 Oct 2023
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 Comments
Nikoleta
Nikoleta on 22 Oct 2023
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 on 22 Oct 2023
@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 ;-)

Sign in to comment.

Answers (2)

John D'Errico
John D'Errico on 23 Oct 2019
Edited: John D'Errico on 23 Oct 2019
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 Comment
CamboSlice
CamboSlice on 17 Dec 2019
I've made this mistake MANY times haha. @John D'Errico answered correctly, albeit slightly harsh.

Sign in to comment.


Nikoleta
Nikoleta on 22 Oct 2023
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 Comments
Image Analyst
Image Analyst on 22 Oct 2023
Edited: Image Analyst on 22 Oct 2023
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 on 22 Oct 2023
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!