how to calculate the distances between points coordinates and the arbitrary reference point using loops?
Show older comments
This solution is solved before within a little change, They have calculated the distances between pairwise coordinate. as you can see inside the link below:
But in my question the X and Y have points inside which are increase sequentially. For example:
Let us consider:
X = [0 2 4 6; 0 2 4 6; 0 2 4 6]; % and
Y = [0 0 0 0; 2 2 2 2; 4 4 4 4; 6 6 6 6];
all of the points inside X and Y are due to the geometry of my model configuration. Let consider a reference point which is inside the model.I need to find the distance between all points and reference point. I know that I should use a loop for the sequential changes but I don't know how?
I should note that the configuration may be changed.
Thank you
Sanaz
Answers (1)
Rik
on 25 Feb 2018
If X and Y contain the coordinates of your sample points, then the code below will give you the Euclidean distance between the fixed point and the sample points.
F_x=3;F_y=3;%coordinates of the fixed point
E_distance=sqrt((X-F_x).^2+(Y-F_y).^2);
I don't really see the connection to the question you linked to, so please leave a comment if this isn't what you mean.
8 Comments
Star Strider
on 25 Feb 2018
Rik
on 25 Feb 2018
What you are looking for is exactly what my code should do. As Star Strider suggested, you can also use the hypot function:
F_x=3;F_y=3;%coordinates of the fixed point
E_distance=hypot(X-F_x,Y-F_y);
Image Analyst
on 25 Feb 2018
Sanaz, if you need another opinion, then I'll add mine. The solution is just as Rik said before you posted your "clarifying" image. Did you even try it? If not, why not. If so, did something not go as expected?
However I admit I'm confused when you say
X = [0 2 4 6; 0 2 4 6; 0 2 4 6]; % and
Y = [0 0 0 0; 2 2 2 2; 4 4 4 4; 6 6 6 6];
so that X is a 3 row by 4 column matrix while Y is a 4 row by 4 column matrix. Exactly what are the "matched up" (x,y) coordinate pairs in that kind of non-equal size matrix layout????
And, once you find all the distances, what do you want to do? Find the min or max distance? Something else?
Sanaz Kb
on 25 Feb 2018
Rik
on 25 Feb 2018
If you need to consider every combination, why don't the lengths match up like this then:
X = [0 2 4 6; 0 2 4 6; 0 2 4 6; 0 2 4 6];
Y = [0 0 0 0; 2 2 2 2; 4 4 4 4; 6 6 6 6];
Rik
on 9 Mar 2018
You can optimize your code like this:
pos_x = 4; % number of the position in x coordinate
pos_y = 3; % number of the position in y coordinate
u = [];
v = [];
for i = 1:pos_x
u = [u, 2*(i-1)];
X = repmat(u,pos_y,1).';
v = [v,2*(i-1)];
Y = repmat(v,pos_y,1).';
end
n = size(X,2);
m = size(Y,2);
D = zeros(n,m);
xo = 0.5; % reference point x coordiante
yo = 0.5; % reference point y coordinate
x_elem=X(1:n);
y_elem=Y(1:m);
[X2,Y2]=meshgrid(x_elem,y_elem);
D=hypot(xo-X2,yo-Y2);
But are you sure you don't mean something like the code below? Because your code results in a D that is not described by pos_x or pos_y.
pos_x = 4; % number of the position in x coordinate
pos_y = 3; % number of the position in y coordinate
xo = 0.5; % reference point x coordiante
yo = 0.5; % reference point y coordinate
u=0:2:(2*(pos_x-1));
v=0:2:(2*(pos_y-1));
[X,Y]=meshgrid(u,v);
D=hypot(xo-X,yo-Y);
If this solves your question, please consider marking it as accepted answer.
Categories
Find more on Code Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!