How can I collect the values that I want ?

3 views (last 30 days)
I have this code;
for k1 = 1:length(X)
for k2 = 1:length(X_inv)
DE(k1,k2) = hypot(X(k1)-X_inv(k2), Y(k1)-Y_inv(k2));
% Euclidean Distance
end
end
It works for finding distances from one point to anothers between [X Y] (X,Y give the position of points) matrix and [X_inv Y_inv] matrix. As you see in the image, X,Y creates the 2nd curve and it starts from below until meet the 1st curve above. At the beginning of 2nd curve, the position of X is greater than X_inv (X > X_inv).
Due to shape of this curve, there occurs X < X_inv. Afterward, (again)value of X starts to get bigger. I want DE(k1,k2) collects the values until the limit condition is
X(k1) <= X_inv(k2) && Y(k1)<=Y_inv(k2).
and I do not want the program stops at the beginning, want to stop when they are close each other.
Afterward, I find the minimum value of DE.
I want to collect the points while X(k1) <= X_inv(k2) && Y(k1)<=Y_inv(k2).
But There is a problem I can not solve. THE PROBLEM is;
When I apply
X(k1) <= X_inv(k2) && Y(k1)<=Y_inv(k2),
my program stops before I want. Because at first, "X > X_inv" Curves:
As you see in the image, X,Y creates the 2nd curve and it starts from below until meet the 1st curve above. At the beginning of 2nd curve, the position of X is greater than X_inv (X > X_inv).
Due to shape of this curve, I want DE(k1,k2) collects the values until the limit is as;
X(k1) <= X_inv(k2) && Y(k1)<=Y_inv(k2).
2nd curve should stop when it reaches the limitation.
  5 Comments
Ender Rencuzogullari
Ender Rencuzogullari on 30 Nov 2015
I will try to get the right result by following your advices. Thanks
Ender Rencuzogullari
Ender Rencuzogullari on 30 Nov 2015
Edited: Ender Rencuzogullari on 30 Nov 2015
Sir, I have failed. Could you please give a hand more. I couldn't use the find command because I jest learned this command

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 30 Nov 2015
You have not failed. You need to experiment with the commands.
I do not have your data, so I created some in order to approximate what I believe you want to do. It includes my previous code to get the nearest points on the two lines:
X = 1:10;
Y = 10-5*[1:10];
X_inv = [1:10]-3;
Y_inv = -10+5*[1:10];
for k1 = 1:length(X)
for k2 = 1:length(X_inv)
DE(k1,k2) = hypot(X(k1)-X_inv(k2), Y(k1)-Y_inv(k2)); % Euclidean Distance
end
end
[DEmin,ix] = min(DE(:));
[K1,K2] = ind2sub(size(DE),ix);
Qpt = [X(K1) Y(K1)]; % Nearest (X,Y) Point
Q_invpt = [X_inv(K2) Y_inv(K2)]; % Nearest (X_inv,Y_inv) Point
nxs_idx = find(Y_inv >= Y_inv(K2)); % Only Retain Indices Of ‘Y_inv’ Points >= Closest Point
figure(1)
plot(X, Y)
hold on
plot(X_inv, Y_inv)
plot(Qpt(1),Qpt(2),'bp', Q_invpt(1),Q_invpt(2),'gp') % Plot Closest Points
plot([Qpt(1); Q_invpt(1)], [Qpt(2); Q_invpt(2)], '-k') % Connect Closest Points
hold off
grid
axis equal
title('Plot Showing Nearest Points')
figure(2)
plot(X, Y)
hold on
plot(X_inv(nxs_idx), Y_inv(nxs_idx)) % Plot (X_inv,Y_inv) Without Overlap
plot([Qpt(1); Q_invpt(1)], [Qpt(2); Q_invpt(2)], '-k') % Connect Lines
hold off
grid
axis equal
title('Plot Connecting Nearest Points & Deleting Others')
  6 Comments
Ender Rencuzogullari
Ender Rencuzogullari on 1 Dec 2015
I could make my program works perfectly if I have knowledge about finding intersection coordinate of two curves. However, I have finished it and it does not work perfect but near the perfect.
Star Strider
Star Strider on 2 Dec 2015
I looked up your earlier Question that had a more complete plot image. It seems that your data are all calculated (I first thought they were experimental measurements), so if you have the equations for both (X,Y) and specifically an expression for Y as a function of X, and (X_inv,Y_inv) and specifically an expression for Y_inv as a function of X_inv, you might be able to use either the fzero function, or if you have the Optimization Toolbox, the fsolve function, to find the intersections. (The fsolve function is a bit more robust.) You might be able to use anonymous functions if they are relatively simple calculations, otherwise you will have to create a function file.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!