How to find the indices that a point lays between?

3 ビュー (過去 30 日間)
ARGY B
ARGY B 2019 年 9 月 11 日
コメント済み: Matt J 2019 年 9 月 12 日
points.PNGpoint.PNG
I have a vector X and a vector Y with 112 points. See picture above, the points are depected with a cross symbol.
There is a point A in the position where the arrow points out that I know its value (xA,yA).
How can I find the points that this point A lays between? For example here the answer would be 83 and 84.
  4 件のコメント
ARGY B
ARGY B 2019 年 9 月 11 日
You are right.
Do you suggest another way? In case X is not increasing.
Bruno Luong
Bruno Luong 2019 年 9 月 11 日
編集済み: Bruno Luong 2019 年 9 月 11 日
If your array are 2-columns: X is n x 2 and Xa is 1 x 2, I propose
d2 = sum((X-Xa).^2,2)
[~,i] = min(d2);
if i==length(d2) || (i > 1 && d2(i-1) < d2(i+1))
i = i-1;
end
point1 = X(i,:);
point2 = X(i+1,:);

サインインしてコメントする。

回答 (2 件)

Matt J
Matt J 2019 年 9 月 11 日
編集済み: Matt J 2019 年 9 月 11 日
Do you suggest another way? In case X is not increasing.
If X,Y are non-monotonic, I would do
[~,points] = pdist2([X(:),Y(:)],[xA,yA] ,'Smallest',2)

Matt J
Matt J 2019 年 9 月 11 日
編集済み: Matt J 2019 年 9 月 11 日
find is inefficient. You should instead do,
point1=discretize(xA,X);
point2=point1+1;
  6 件のコメント
Bruno Luong
Bruno Luong 2019 年 9 月 12 日
>> X=10:-1:1
X =
10 9 8 7 6 5 4 3 2 1
>> xA=2.5 % data fall between 3 and 2, meaning indexes 8 and 9 of X
xA =
2.5000
>> numel(X) + 1 - discretize(xA,flip(X)) % this should be index of Point 2
ans =
9
>> discretize(-xA,-X) % this should be index of Point 1
ans =
8
Matt J
Matt J 2019 年 9 月 12 日
Ah. Yes, you are right.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by