Conditioning data to match dimensions

1 view (last 30 days)
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

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 4 Apr 2013
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);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!