How can I collect the values that I want ?

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

I do not have your data, but if I understand your Question correctly, one option would be to use the find function to return the index values of the minimum y-value for ‘1st Curve’ and use it again to find the maximum y-value of ‘2nd Curve’. Then use those indices to get the corresponding x-values.
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
with this code, I find the distance among every points on 1st curve to 2nd curve and collect them. Then;
[DEmin,ix] = min(DE(:));
[K1,K2] = ind2sub(size(DE),ix);
fprintf(1,'\nNearest Points:\n\tDistance = %.4f mm\n', DEmin)
fprintf(1,'\t\tX(%d), Y(%d) \t\t\t= %.4f mm, %.4f mm\n',K1, K1, X(K1), Y(K1))
fprintf(1,'\t\tX_inv(%d), Y_inv(%d) \t= %.4f mm , %.4f mm \n', K2, K2, X_inv(K2), Y_inv(K2))
with this code, I find the nearest point on 1st curve to 2nd curve from collected values in DE(k1,k2).
If you see the image; second curve is passing the first curve. Because the nearest point in 2nd curve to 1st curve is located at this point.( I upload the new and big image.) (by the way, you wrote this code yesterday )
But I want to collect the values of DE(k1,k2) when the limit comes up which is;
X(k1) <= X_inv(k2) && Y(k1)<=Y_inv(k2)
In fact, I want to find the nearest point under this condition
I remember it!
One possibility is for you to use min, max (both with two outputs) or find to get the minimum y-value of 1st curve and the maximum of 2nd curve and their indices, and then use those to define the (x,y) values of the other curve.
It would seem that the nearest values of the two would correspond to the values you want to connect, so I would use those k1 and k2 values to define them.
I will try to get the right result by following your advices. Thanks
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

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

Dear Sir,
First of all, I want to send special thanks to you. I customized for my program and I did learn something. My code turned into;
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);
nxs_idx= find( Y <= Y_inv(K2));
for i=1: length(X(nxs_idx))
for j=1: length(X_inv)
D_E(i,j) = hypot(X(i)-X_inv(j), Y(i)-Y_inv(j)); % Eucledian Distance
end
end
[D_Emin,i_x] = min(D_E(:));
[K_1,K_2] = ind2sub(size(D_E),i_x);
Qpt = [X(K_1) Y(K_1)]; % Nearest (X,Y) Point
Q_invpt = [X_inv(K2) Y_inv(K2)]; % Nearest (X_inv,Y_inv) Point
I can find the points of Y;
Y <= Y_inv(K2);
But at the same time X(end) <= X_inv(K2) must be provided.
When the program does not provide the X(end) <= X_inv(K2) condition, even if the program find and plot Y(end)<=Y(K2), the X(end) might be greater than X(K2). And I cannot get the right shape (image is above). I need to hold the second curve's last point inside of 1st curve.
I cannot use the find( X<=X_inv(K2) & Y <= Y_inv(K2);) because at first, X > X_inv(K2) so program stops at the beginnig.
%After nxs_idx= find( Y <= Y_inv(K2));
I tried;
if X(end) > X_inv(K2)
nxs_idxx = nxs_idx;
nxs_idxx(end)= [];
end
nothing came true.
My pleasure.
Since I do not have your data, I cannot write specific code for it. My intent was to outline the general idea, demonstrate that it does what I believe you want it to do, then for you to experiment with it to modify it to work with your data.
Experiment also by switching (X,Y) for (X_inv,Y_inv) in my code. The easiest way to do that would be to create a function from my code (see the documentation in the relevant sections of Function Basics if necessary), and pass the appropriate vectors as arguments to it.
That is the best I can do.
I appreciate. I have learned so many things with your favour.
As always, my pleasure. Thank you.
I could have been more help if I had your data to work with. I will still help as I can.
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.
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)

Categories

Products

Community Treasure Hunt

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

Start Hunting!