# Determine the relational operator in an expression

조회 수: 1 (최근 30일)
Geovane Gomes 2023년 11월 15일
답변: Les Beckham 2023년 11월 16일
I need to find out the operator in an expression.
I tried to convert the symbolic expression using char and then find the position of the operator ("<=","==",">="). However, when using the char function, Matlab always puts it in the form "<=" when it is an inequality. Therefore I never find the gt operator.
syms x y
exp1 = x <= y;
exp2 = x == y;
exp3 = x >= y;
charExp = char([exp1,exp2,exp3])
charExp = '[x <= y, x == y, y <= x]'
##### 댓글 수: 2없음 표시없음 숨기기
John D'Errico 2023년 11월 15일
syms x y
exp1 = x <= y
exp1 =
exp2 = x == y
exp2 =
exp3 = x >= y
exp3 =
But it also flipped the left and right hand sides. So the two are equivalent. All that matters is what is on each side.
Torsten 2023년 11월 15일
Your expressions already have to be of type "char", not of type "sym", to extract the operator used.

댓글을 달려면 로그인하십시오.

### 답변 (3개)

Pooja Kumari 2023년 11월 15일
편집: Pooja Kumari 2023년 11월 15일
Dear Geovane,
It is my understanding that you are facing issues with converting symbolic expression using "char".
It seems that the "char" function in MATLAB always represents inequalities using the "<=" operator, regardless of the original operator. Unfortunately, there is no direct way to obtain the original ">=" operator using the "char" function. It is clear that "exp3" will give "x>=y" but it switched to "<=" which is correct expression as shown below:
syms x y
exp3 = x >= y;
char(exp3)
ans = 'y <= x'
Regards,
Pooja
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Image Analyst 2023년 11월 15일
Then you can use contains to run down the various math operators you might possibly encounter, and then take appropriate action
syms x y
exp3 = x >= y;
% Cast to string.
thisExpression = char(exp3)
thisExpression = 'y <= x'
if contains(thisExpression, '>=')
% Operator is >= so do something with that knowledge.
fprintf('Operator is >=.\n');
elseif contains(thisExpression, '>')
% Operator is > so do something with that knowledge.
fprintf('Operator is >.\n');
elseif contains(thisExpression, '<=')
% Operator is <= so do something with that knowledge.
fprintf('Operator is <=.\n');
elseif contains(thisExpression, '<')
% Operator is > so do something with that knowledge.
fprintf('Operator is <.\n');
elseif contains(thisExpression, '~=')
% Operator is ~= so do something with that knowledge.
fprintf('Operator is ~=.\n');
% etc for the other possible operators.
end
Operator is <=.
##### 댓글 수: 7이전 댓글 5개 표시이전 댓글 5개 숨기기
Image Analyst 2023년 11월 16일
편집: Image Analyst 2023년 11월 16일
@Geovane Gomes not sure why you didn't yet, but could you explicitly answer @Steven Lord's question: "Can you say more about what you're hoping to do that requires you to distinguish those two mathematically equivalent statements?" Do you just want to get the coefficients? But if you have the expression, you already know the coefficients. Or is your expressions comcing to you somehow in some mysterious method where you don't know the coefficients yet? If so, what exactly might be an input that you need to inspect to find the coefficients?
Geovane Gomes 2023년 11월 16일
In order to make it more "friendly", I would like the expressions as input, not only its coefficients.
@Walter Roberson's solution is enough for me.
Thanks!

댓글을 달려면 로그인하십시오.

Les Beckham 2023년 11월 16일
If you can rely on x always being on the left in your "test" expressions, you can test for y being on the left in the result returned by char (to detect that the symbolic toolbox reversed the order of the comparison). For example:
syms x y
exp1 = x <= y;
exp2 = x == y;
exp3 = x >= y;
charExp3 = char(exp3)
charExp3 = 'y <= x'
if contains(charExp3, '<=')
if charExp3(1) == 'x'
operator = '<='
else
operator = '>=' % if symbolic toolbox swapped the comparison, swap the result
end
end
operator = '>='

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Common Operations에 대해 자세히 알아보기

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by