# How can I collect the values that I want ?

1 view (last 30 days)

Show older comments

Ender Rencuzogullari
on 30 Nov 2015

Commented: Star Strider
on 2 Dec 2015

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

### Accepted Answer

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

Star Strider
on 2 Dec 2015

### More Answers (0)

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!