Conditioning data to match dimensions

1 回表示 (過去 30 日間)
KV
KV 2013 年 4 月 4 日
Hi, I have a data set which is 1 x 8. I have two other sets of data B and C which are 1 x 130. What I want to do is, compare the values of data set B with that of A to find the closest element. Once the index of this element of B is known, I want to read the corresponding element of that index from C. I want to do this with every element and create a new matrix based on elements from C and should be 1 x 8.
I was thinking along the lines of using a for loop and then using something like:
for f= 1:8;
tmp = abs(b-a(1,f));
[idx idx] = min(tmp) %index of closest value
%%more code needed
end
I am bit lost after that about how I can efficiently implement this. Any ideas?
Thanks

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 4 月 4 日
A = rand(10,1);
B = rand(130,1);
C = randi(500,130,1);
a1 = A(1:8);
[~,idx] = min(abs(bsxfun(@minus,B,a1.')));
out = C(idx);
  1 件のコメント
KV
KV 2013 年 4 月 8 日
Just what I needed. Many thanks!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by