Can anyone help me in this code ?

3 次查看(过去 30 天)
%To Calculate the points which lie inside and outside Tolerance
[in,on] = inpolygon(real,imag,xch_larger,ych_larger);
p=numel(real(in))
%disp(in)
q=numel(real(on))
%disp(on)
r=numel(real(~in))
i want a msg to be displayed when r>=0
i have put this statement
if r >= 0;
msgbox('So-n-so','Detected Object','error');
else
msgbox('So-n-so','Detected Object');
end
But the problem i am facing is, even if the value is r<=0 Still it displays the first msg, it does not go in the else part... what is problem ?

采纳的回答

Guillaume
Guillaume 2017-3-15
编辑:Guillaume 2017-3-15
I strongly suspect that you want the number of query points in, on and outside the polygon, which you'll never get with numel (See comment to Image Analyst's answer). I suspect this is what you want:
p = sum(in); %number of points inside or on the polygon
q = sum(on); %number of points on the polygon
r = sum(~in); %number of points outside the polygon
  3 个评论
Guillaume
Guillaume 2017-3-15
I've just noticed that real and imag are variable names, and not the functions real and imag. So yes, your code would work (but be slightly slower).
However, I would encourage you to rename these two variables to something that is not a standard matlab function. Not only would it avoid this sort of confusion, it would prevent potential bugs (where you try to use the function and ends up indexing into the variable instead).
L K
L K 2017-3-16
Thanx for the valuable info :) I did the changes

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-3-15
numel() returns the number of elements in an array. Why do you think that should EVER be negative?
  2 个评论
L K
L K 2017-3-15
Ok Ya i got my mistake... if was suppose to be r>0... and the other condition would be true when r=0... I just realized it..
Guillaume
Guillaume 2017-3-15
编辑:Guillaume 2017-3-15
And since in and on are logical arrays, all those real(something) are also completely pointless.
And since in and ~in obviously have the same numbers of elements, p and r will always be equal.
"and the other condition would be true when r=0"
And since the number of elements in in and on is the same as the number of elements in the input, the only way that r can be 0 is if you pass zero query points.
Really, the problem is not with the if but with everything that is before!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by