How to draw a perfect circle and distance calculation

Hi,
I got 2 issues:
  1. to draw a perfect circle
  2. to calculate a perfect distance
This is my code:
load fisheriris
rng(1); % For reproducibility
n = size(meas,1);
idx = randsample(n,1)
X = meas(~ismember(1:n,idx),3:4); % Training data
Y = meas(idx,3:4)
MdlKDT = KDTreeSearcher(X) % not used by mahanalobis
MdlES = ExhaustiveSearcher(X) % Prepare an exhaustive nearest neighbors searcher.
r = 0.3; % Search radius
IdxKDT = rangesearch(MdlKDT,Y,r)
IdxES = rangesearch(MdlES,Y,r)
[IdxKDT IdxES]
cellfun(@isequal,IdxKDT,IdxES)
figure;
plot(X(:,1),X(:,2),'.k');
hold on;
plot(Y(:,1),Y(:,2),'*r');
Mdl = KDTreeSearcher(X)
[n,d] = knnsearch(Mdl,Y,'k',10);
ctr = Y - d(end);
diameter = 2*d(end);
% Draw a circle around the 10 nearest neighbors.
h = rectangle('position',[ctr,diameter,diameter],...
'curvature',[1 1]);
h.LineStyle = ':';
If i used the fisheriris data, it is works well. But if i'm using Faithful Geyser data (the data as in the attachment), the circle becomes weird. The distance calculation as shown in second picture with yellow color also effected where the data points do not lie within a circle.. This is the code to apply the faithful data:
faithfuldat = xlsread('faithful.csv');
fData = faithfuldat(:,:);
[n,dim]=size(fData);
idx = randsample(n,1)
X = fData(~ismember(1:n,idx),:); % Training data
Y = fData(idx,:)
I really appreciate if anyone could advice me how to improve the circle and distance.

 채택된 답변

Image Analyst
Image Analyst 2018년 3월 9일
편집: Image Analyst 2018년 3월 9일
To draw a circle, you can use rectangle(), viscircles(), or the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
Add
axis equal;
to your code after you plot to make your circles look circular on the screen.

댓글 수: 11

amj
amj 2018년 3월 9일
편집: amj 2018년 3월 9일
Dear Image Analyst,
I had applied axis equal and i got this output:
I also referred to the given link but it does not work. This is the codes:
for j = 1:length(Y)
c = Y(j,:);
circleFun = @(x1,x2)r^2 - (x1 - c(1)).^2 - (x2 - c(2)).^2;
%%option 1
%%------
%fimplicit(circleFun,[c(1) + [-1 1]*r, c(2) + [-1 1]*r]);
%%option 2
%%------
%ezplot(circleFun,[c(1) + [-1 1]*r, c(2) + [-1 1]*r]);
%%option 3
%%------
% radius = r;
% centerX = c(1);
% centerY = c(2);
% rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],...
% 'Curvature',[1,1],...
% 'FaceColor','r');
%%option 4
%%------
% c = [3, 4];
% %r = 2;
% pos = [c-r, 2*r 2*r];
% annotation('ellipse', pos );
end
All the options code yield output as above. Really appreciate if i can get any advice.
OK. Looks right - your circle is now round after using "axis" equal. Thanks for Accepting the answer.
But you may want to get rid of so much plot along the x axis by using xlim():
xlim([0, 10]);
Thank you Image Analyst,
Let me try
Dear Image Analyst, It is still appears the same result.
Looks like you didn't call axis equal on that plot so your circles still show up as ellipses on the screen, even though they are circles in the data space. Obviously if you stretch your graph one way or the other, your circles will change shape. I hope you understand this.
Dear Image Analyst, I really do apologize. I dont get your points. Or does it mean, the min / max on axis based on the data?
You didn't call axis equal so the scales on x and y don't match. Look at your y axis - it goes from 40 to 100, a range of 60. Now look at your x axis - it goes from 1 to 6, a range of 5. So your x axis is magnified compared to your y axis. Let's say you have a circle centered at x=2, y=60 with radius 1. So the circle goes from 1 to 3, a diameter of 2 which is 1/3 of the x axis range. But in the vertical direction, the circle goes from 59 to 61, again a range of 2. However 2 is only 1/30 of the vertical y axis size, not 1/3 so it's much, much narrower in the y direction. That's why it looks like an ellipse. Basically you're squashing your graph vertically to get it to fit in the same space as your horizontal space. So the circle gets squashed. If you do axis equal, it prevents squashing and the circle will truly be a circle.
Yeah, it is indeed true. It is because the faithful data has different range of data compared to Iris data. I have been exploring on how to improve/combine with the range, still could not find the best solution
If you want it to display as a circle despite the axes not being equal, you have to scale the height of the circle in rectangle() by the ratio of the two axes lengths, which you can get with xlim() and ylim().
Dear image analsyt,if so, i would try tomorrow. i have been trying since his morning. I'm still could not fix it
Dear Image Analyst,
I modified the code:
xx = (range(diff(X(:,1))))
yy = (range(diff(X(:,2))))
ratio = (xx / yy) /sqrt(2)
for j = 1:length(Y)
c = Y(j,:)
Yx = c(1);
Yy = c(2);
pos = [c-r (2*r) (2*r)/ratio];
rectangle('Position',pos,'Curvature',[1 1])
end
axis([1 5.5 45 95])
Unluckily, the central points are not lie in center part. If you could advice an improvement, i would be grateful

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

질문:

amj
2018년 3월 9일

댓글:

amj
2018년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by