이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Does anyone know how to use the matlab to calculate the minimu distance between a point outside oval and the oval surface?
조회 수: 12 (최근 30일)
이전 댓글 표시
Does anyone know how to use the matlab to calculate the minimu distance between a point outside oval and the oval surface?
댓글 수: 3
huazai2020
2020년 6월 29일
The oual can be both given in the form of an equation or data points. How to use the bwdist(), could you share me the code?
채택된 답변
Matt J
2020년 6월 30일
편집: Matt J
2020년 6월 30일
You can use trustregprob from the File Exchange
For example, consider the following ellipse and external point y,
A=[2 1;1,2]; %Ellipse equation matrix
y=[1;0.5]; %External point
z=[0.584808315593597 ; 0.201052451754066]; %Closest point
ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1 ;
hold on
fimplicit(ellipsefun, [-1 1.3 -1 1.3])
plot(y(1),y(2),'rx',z(1),z(2),'bo');
axis equal
hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/324112/image.png)
I found the closest point z using the code below,
Aroot=chol(A);
L=Aroot\eye(2);
z=Aroot\trustregprob(L.'*L, L.'*y,1);
and the minimum distance is just,
>> distance=norm(z-y)
distance =
0.5116
댓글 수: 20
huazai2020
2020년 7월 3일
How can you know the Closest point(z=[0.584808315593597 ; 0.201052451754066]; %) before calculation?
huazai2020
2020년 7월 4일
Thank you,because I am not familar with the codes,would you please paste them in a right sequence?
Matt J
2020년 7월 4일
A=[2 1;1,2]; %Ellipse equation matrix
y=[1;0.5]; %External point
Aroot=chol(A);
L=Aroot\eye(2);
z=Aroot\trustregprob(L.'*L, L.'*y,1);
huazai2020
2020년 7월 4일
This is codes I use:
clc; clear all;
A=[2 1;1,2]; %Ellipse equation matrix
y=[1;0.5]; %External point
Aroot=chol(A);
L=Aroot\eye(2);
z=Aroot\trustregprob(L.'*L, L.'*y,1);
ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1 ;
hold on
fimplicit(ellipsefun, [-1 1.3 -1 1.3])
plot(y(1),y(2),'rx',z(1),z(2),'bo');
axis equal
hold off
I have the error like this:
Undefined function 'fimplicit' for input arguments of type 'function_handle'.
huazai2020
2020년 7월 4일
In addition, I do not what is the meaning of “ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1 ;”, I cannot find in my
matlab.
Matt J
2020년 7월 4일
편집: Matt J
2020년 7월 4일
Undefined function 'fimplicit' for input arguments of type 'function_handle'.
You obviously have an older version of Matlab, but enough of the code should have run to give you z, which is the answer you are looking for. The rest is just for plotting.
I do not what is the meaning of “ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1
That is the function that desribes the ellipse. If you had a newer version of Matlab, you would be able to plot the ellipse using
fimplicit(ellipsefun,...)
huazai2020
2020년 7월 5일
Thank, I use the matlab R2012b, do there have any other substitution equation for the fimplicit function, or how could I revise it?
Second, I have not understood the meaning of “@(p,q) sum([p;q].*(A*[p;q]))-1 ;” , do you have any discribe of it? If my oval is x^2+y^2/4=1, how could I revise the oval equation?
Matt J
2020년 7월 5일
편집: Matt J
2020년 7월 5일
For R21012b, you can use ezplot instead. Both of the following are equivalent ways to write the formula for your ellipse and plot it.
figure
ezplot( 'x^2+y^2/4-1')
figure
A=[1,0;0,1/4];
ezplot(@(p,q) sum([p.';q.'].*(A*[p.';q.']),1)-1) %same as x^2+y^2/4-1 = 0
To uderstand the 2nd form better, you can read,
Matt J
2020년 7월 6일
편집: Matt J
2020년 7월 6일
Those are the operations that the function executes. All points (p,q) on the ellipse must satisfy,
sum([p.';q.'].*(A*[p.';q.']),1)-1 == 0
This is, in any case, quite a separate question from the one you originally posted, which was about how to find z. If you are now comfortably projecting points onto ellipses with an Answer from this thread, I suggest that you Accept-click the answer and pose your plotting questions in a separate post.
huazai2020
2020년 7월 6일
Could you tell me how what is the meaning of "sum([p.';q.'].*(A*[p.';q.']),1)-1 == 0"
Matt J
2020년 7월 6일
Once again, it is the equation for the ellipse. It is exactly the same as writing
. And once again, if you are still having trouble with the concept, you should post it as a new question, ideally with some elaboration on what is unclear and what needs explanation.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/327722/image.png)
huazai2020
2020년 7월 6일
Could you show me the link that describe the equation ? I want to see the meeaning of it.
Bruno Luong
2020년 7월 7일
In the FEX I post bellow there are 2 functions that convert between ellipse parametric explicit eqauation to quadratic implicit equation used by Matt.
Read the H1 line of those functions to get a flavor, and read what it does exactly in their body. The math behind is quadratic form.
huazai2020
2020년 7월 7일
What is FEX AND H1? Are they these?
Find the projection of point P in R^n on the ellipsoid
E = { x = x0 + U*(z.*radii) : |z| = 1 }, where U is orthogonal matrix of the orientation of E, radii are the axis lengths, and x0 is the center.
Or on generalized conic E = { x : x'*A*x + b'*x + c = 0 }.
The projection is the minimization problem:
min | x - P | (or max | x - P|) for x in E.
Method: solve the Euler Lagrange equation with respect to the Lagrange multiplier, which can be written as polynomial equation (from an idea by Roger Stafford)
Bruno Luong
2020년 7월 8일
FEX short word for File Exchange. Click on it I put the link on my message.
H1: https://www.mathworks.com/help/matlab/matlab_prog/add-help-for-your-program.html
추가 답변 (2개)
Image Analyst
2020년 7월 4일
"The oual can be both given in the form of an equation or data points." <== if you have data points (xb, yb) on the boundary of an ellipse/oval, you can use sqrt() to find the distances to some other point (xp, yp). Then use in() to find the minimum distance.
distances = sqrt(xb-xp).^2 + (yb-yp).^2);
minDistance = min(distances)
If you have an image then you need to get the boundary points first:
boundaries = bwboundaries(binaryImageOfEllipse);
boundaries = boundaries{1}; % Extract double array from cell array.
xb = boundaries(:, 2);
yb = boundaries(:, 1);
댓글 수: 10
huazai2020
2020년 7월 4일
For the input method, I have input the data points and boundary points(only a half oval), would you please share me the code to calculate the minimum distance?
For the second method, I do not have image,but I know the the equation of oval ,how can I do it?
Image Analyst
2020년 7월 4일
outpoint has two worksheets in it, and walldata has 1. I'm not sure what's what or what's where. Which is the single point, and which columns are the (x,y) boundary points? Please give me code to read in the point (xp, yp) and list of (xb,yb) boundary points. But after that, it's just like I said:
distances = sqrt(xb-xp).^2 + (yb-yp).^2);
minDistance = min(distances)
huazai2020
2020년 7월 5일
I have noted the x and y on the top of columns , you can see that in the files.
I have solved the problem. Could you tell me if I do not know the boundary points, how can I calculation the minimum distance by the oval equation?
Image Analyst
2020년 7월 5일
I don't know off the top of my head. If all you have is a point and an analytical equation for the ellipse, that is a geometry question and I'd just Google "point ellipse distance" and see what hits you get.
huazai2020
2020년 7월 8일
Hi, after I get the minDistance, I also want to know the wall point coordinate, how could I do it?
Image Analyst
2020년 7월 8일
Again, I don't know what to do for the analytical equation situation, like in answer you accepted. I'd only know what to do for the situation where you have an array for the ellipse boundary and the point coordinates, but you said you'd solved that one already.
huazai2020
2020년 7월 9일
Yes, I just want to ask how to get the wall point coordinate when I use the array for the ellipse boundary and the point coordinates.
huazai2020
2020년 7월 18일
편집: Image Analyst
2020년 7월 18일
Hi. This is the code that I write to find the minimum distance between the points (column A and column B in the data1.xlsx) with the wall (column C and column D in the data1.xlsx), but it has errors. Who can help me? Thank you so much.
clc;
clear all;
A = xlsread('data1.xlsx');
xpoint=A(:,1);
ypoint=A(:,2);
Num1=length(xpoint);
xwoint=A(:,3);
ywoint=A(:,4);
Num2=length(xwoint);
for i=1:1:Num1;
x1=xpoint(i);
y1=ypoint(i);
for j=1:1:Num2;
x2=xwoint(j);
y2=ywoint(j);
distances = sqrt((x2-x1).^2 + (y2-y1).^2);
minDistance(i) = min(distances)*1000;
end
fdata=minDistance;
end
xlswrite('thick.xlsx',fdata,'sheet1');
%%
The below is the errors:
Error using xlswrite (line 220)
Excel returned: Error: Object returned error code: 0x800A03EC.
Image Analyst
2020년 7월 18일
No, you shouldn't use an inner loop because your minDistance will just be the distance between (x1,y1) and xwoint(Num2) and ywoint(Num2) instead of the whole array. Do it like this:
clc;
clear all;
A = xlsread('data1.xlsx');
xpoint=A(:,1);
ypoint=A(:,2);
Num1=length(xpoint);
xwoint=A(:,3);
ywoint=A(:,4);
Num2=length(xwoint);
for k=1:1:Num1
x1 = xpoint(k);
y1 = ypoint(k);
distances = sqrt((xwoint-x1).^2 + (ywoint-y1).^2);
minDistance(k) = min(distances)*1000;
end
plot(minDistance, 'b-');
% xlswrite('thick.xlsx',minDistance,'sheet1');
Not sure why you wanted to multiply by 1000 though.
huazai2020
2020년 7월 18일
편집: huazai2020
2020년 7월 18일
1000 is only for the unit changing. Thank you so much for your help.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)