Conditioning data to match dimensions

1 次查看(过去 30 天)
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by