finding the nearest valaue

1 回表示 (過去 30 日間)
Pat
Pat 2012 年 11 月 19 日
I have two sets of value
s=
38.9906 27.8590 18.6908 25.0184 27.5565 24.7551
21.6141 30.9522 42.2398 52.9675 38.4323 45.0781
39.3954 41.1888 39.0694 22.0141 34.0112 30.1668
S1= 19.0865
36.2719
44.6415
I HAVE to compare 1st value of S1 with first values of s and display them
for example the nearest value in s is
18.6908
42.2398
39.0694
please help
  1 件のコメント
Image Analyst
Image Analyst 2012 年 11 月 19 日
編集済み: Image Analyst 2012 年 11 月 19 日
What do you mean by the " first values of s"? It looks like you're identifying the entire column that contains the value of s closest to the first value of S1, so what does "first values of s" mean to you? To me the first value of s would be 38.9906.
By the way, is this your homework? (Sounds like a homework problem.)

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

採用された回答

Matt J
Matt J 2012 年 11 月 19 日
HINT:
abs(bsxfun(@minus,s,S1));
  1 件のコメント
Jan
Jan 2012 年 11 月 19 日
編集済み: Jan 2012 年 11 月 19 日
+1: A good hint for a homework question. Thanks!
The term "nearest" is usually connected to the distance, e.g. the Euclidean norm.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 19 日
s=[ 38.9906 27.8590 18.6908 25.0184 27.5565 24.7551
21.6141 30.9522 42.2398 52.9675 38.4323 45.0781
39.3954 41.1888 39.0694 22.0141 34.0112 30.1668]
s1= [19.0865
36.2719
44.6415]
out=arrayfun(@(x) find(abs(s-s1(x))==min(min(abs(s-s1(x))))),1:numel(s1),'un',0)
out=s(cell2mat(out)')

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by