Invalid use of operator

조회 수: 140 (최근 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에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by