Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我在 matlab 中有 2 个矩阵:
A = [1 4 6 9 11 13 15 18 21] B = [2 10 19]
有没有我可以使用的函数,以便对于 B 中的每个元素,我能够找到最接近 A 中该元素的值的索引。例如,在上面的示例中:2,10 和 19 在数字上最接近A中的1,9和18,1,9和18的索引是1,4和8,所以函数应该返回[1 4 8]。
我知道我可以使用循环来做到这一点,但 matlab 并不真正喜欢循环,而且我的矩阵太大,并且迭代所有值会在时间上非常昂贵。
我将按如下方式进行:
% clc,clear all,close all A = [1 4 6 9 11 13 15 18 21]; B = [2 10 19]; C = abs(bsxfun(@minus,A',B)); [~,idx] = min(C(:,1:size(C,2)))