Comparing 2 vectors: finding closest point

9 ビュー (過去 30 日間)
Jared
Jared 2013 年 3 月 12 日
If I have 2 vectors, say:
x=[1 2 3 4 5];
y=[0 1.1 1.5 1.6 2.4 4.1 4.7 5 5.7];
I want to find the index of the closest match in y, for each index in x. So, for x(1) it would be y(2), for x(2) it would be y(5), etc. Also, it would be nice to have the counts of y values for each x index that occur before the next x index. For example, x(1) it would be 3 (1.1, 1.5, 1.6). x(3) it would be 0.
I was trying to use histc for this, with x specifying the edges, but didn't know if there was a different approach.

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 12 日
編集済み: Azzi Abdelmalek 2013 年 3 月 12 日
x=[1 2 3 4 5];
y=[0 1.1 1.5 1.6 2.4 4.1 4.7 5 5.7];
for k=1:numel(x)
[val,idx]=min(abs(y-x(k)));
xx(k)=y(idx);
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by