Looped vector calculation and storage of answer

Hello, I have two horizontal vectors which contain points for plotting a circle; vector "x" for x-coordinates and vector "y" for y-coordinates. I am trying to write a piece of code which will determine the largest chord within that circle. The approach I am taking is to use the distance formula (sqrt((x2-x1)^2)+((y2-y1)^2)) and calculate the distance between the first point and the rest - store all the distances. Then the distance between the second point and the rest - store all distance to same place. And continue this till the last point. In the end I shall have a vector (or matrix) which contains all these distances, I can then run a "max" command to find the maximum distance. I am just not sure how to implement this into MatLab, in a neat looped script. All and any help will be greatly appreciated.

 Respuesta aceptada

Matt J
Matt J el 11 de Jul. de 2013
Editada: Matt J el 11 de Jul. de 2013
You can use my interdists() function below. Since you say it is a circle, I don't see why you would want to compare all pairs of points. The only way this can give you a good approximation of the diameter is if the circle is well-sampled, and in that case, the distance to one particular point ,e.g.,
data=[x;y];
distances = interdists(data(:,1),data);
ought to be enough.
function Graph=interdists(A,B)
%Finds the matrix of distances between point coordinates
%
% (1) Graph=interdists(A,B)
%
% in:
%
% A: matrix whose columns are coordinates of points, for example
% [[x1;y1;z1], [x2;y2;z2] ,..., [xM;yM;zM]]
% but the columns may be points in a space of any dimension, not just 3D.
%
% B: A second matrix whose columns are coordinates of points in the same
% Euclidean space. Default B=A.
%
%
% out:
%
% Graph: The MxN matrix of separation distances in l2 norm between the coordinates.
% Namely, Graph(i,j) will be the distance between A(:,i) and B(:,j).
%
%
% (2) interdists(A,'noself') is the same as interdists(A), except the output
% diagonals will be NaN instead of zero. Hence, for example, operations
% like min(interdists(A,'noself')) will ignore self-distances.
%
% See also getgraph
noself=false;
if nargin<2
B=A;
elseif ischar(B)&&strcmpi(B,'noself')
noself=true;
B=A;
end
N=size(A,1);
B=reshape(B,N,1,[]);
Graph=l2norm(bsxfun(@minus, A, B),1);
Graph=squeeze(Graph);
if noself
n=length(Graph);
Graph(linspace(1,n^2,n))=nan;

7 comentarios

Mazhar
Mazhar el 12 de Jul. de 2013
Thank you Matt for your quick response. The reason I am needing to do this for all points is because I cannot be sure that the "circle" is completely circular, it could be deformed in to an oval or parts of it deformed like a 'dent' in the circle. That is why I will need to check every chord for the largest.
OK, but it's clear to you that you can still do that with the utility I've presented, right? To get the full matrix, just do
distances = interdists(data);
Mazhar
Mazhar el 12 de Jul. de 2013
I am trying to make the code work, but so far no luck. My only inputs at the moment are; vector x and vector y. I'm not quite understanding the variables 'A' and 'B' here.
Matt J
Matt J el 12 de Jul. de 2013
Editada: Matt J el 12 de Jul. de 2013
The columns of A and B are your coordinates and you can omit B if A=B. For example, this gives the matrix of distances between the columns of eye(2)
>> interdists(eye(2))
ans =
0 1.4142
1.4142 0
Mazhar
Mazhar el 12 de Jul. de 2013
Ok, I see. I've replaced A and B with my vectors. If A and B represent my data, then why use x,y for the line 'data=[x;y];'? Also when I try to run the script I am getting an error for line 36: "Graph=l2norm(bsxfun(@minus, x_raw, y_raw),1);" "Undefined function 'l2norm' for input arguments of type 'double'"
Mazhar
Mazhar el 12 de Jul. de 2013
Say I have these values; x=[6 5.5 4 3 2 0 -2 -3 -4 -5 -5.5 -6 -5.5 -5 -4 -3 -2 0 2 3 4 5 5.5 6]; y=[0 2 3.5 4.5 5 5.5 6 5.5 5 4.5 3.5 2 0 -2 -3.5 -4.5 -5 -5.5 -6 -5.5 -5 -4.5 -3.5 -2 0]; How would I use your code to get the out put I want? Or how can I use a Loop for this!
Mazhar
Mazhar el 12 de Jul. de 2013
Got it to work :D Thanks Pal!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by