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
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!