How can I collect the values that I want ?
1 回表示 (過去 30 日間)
古いコメントを表示
Ender Rencuzogullari
2015 年 11 月 30 日
コメント済み: Star Strider
2015 年 12 月 2 日
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 件のコメント
採用された回答
Star Strider
2015 年 11 月 30 日
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 件のコメント
Star Strider
2015 年 12 月 2 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!