in algorithm a~b , in MATLAB...??????

Hi everybody, In an algorithm I am working on, I came to a check box with content:
a~b
where the '~' would be read as "a approximates b" which can be defined probably by absolute numerical difference. so if a~b=TRUE (i.e the difference b/n a and b is very small)further computations are done if not( i.e. a~b=FALSE) then termination! , if 'YES' it continues with other computations, and if 'NO' it terminates. How can I write this in MATLAB?

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 10월 19일
In that context, it would be read as "a approximates b". But like the others say below, you need to define what approximation means in the context.

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

 채택된 답변

Daniel Shub
Daniel Shub 2011년 10월 19일

0 개 추천

Assuming ~ mean not (or not equal) in your algorithm you could use ~= or ~isequal(). I prefer the isequal approach since it handles arrays and mismatched sizes ...
if isequal(a, b)
disp('NO')
else
disp('YES')
end
Although it seems a little backwards to me that a~b leads to YES.

댓글 수: 5

Meh
Meh 2011년 10월 19일
Thanx Dani, just some more clarifications and corrections. in my algorithm the ~ looks more of closeness between a and b; so if a~b=TRUE (i.e the difference b/n a and b is very small)further computations are done if not( i.e. a~b=FALSE) then termination!
Daniel Shub
Daniel Shub 2011년 10월 19일
In that case replace isequal(a, b) with abs(a-b) > tol. Where tol is whatever tolerance your want. This requires a and b to be scalars.
Jan
Jan 2011년 10월 19일
@Meh: Please define "very small". Do you mean the absolute or relative numerical difference? Or are "a" and "b" strings and you search for the edit-distance? Or are "a" and "b" lines in 3D and you search for the shortest connection?
It would be very helpful, if you explain the term "a~b" exactly by editing the original question - not as comment of an answer. Thanks.
Jan
Jan 2011년 10월 28일
@Meh: After you have clarified that "~" should mean "approximation", it got clear, that this amswer does not concern the question. Why do you accept it then as a solution?
It would be helpful for the forum, if you explain what "approximation" means qualitatively for your case. Then an answer would be very easy.
Daniel Shub
Daniel Shub 2011년 10월 28일
It might also be helpful if I edited my answer to reflect the comment I made about comparing the difference to a tolerance. I am hesitant to do that since the answer has already been accepted.

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

추가 답변 (0개)

질문:

Meh
2011년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by