我有矩阵 [m x n] 和向量 [m],我想将每一行与相应的向量数进行比较,有没有办法实现这种向量化方法?
使用
bsxfun
:
% example data
M = rand(5, 3);
V = rand(5,1);
% for equality (==) :
bsxfun(@eq, V, M);
% for greater-than (>) :
bsxfun(@gt, V, M);
% for greater-than-or-equals (>=) :
bsxfun(@ge, V, M);
等等。可用功能列表在
help bsxfun
中列出。
您可以尝试使用
intersect
函数来比较矩阵中是否有向量成员。
然后将这些成员保存到一个新的向量中(如果这对您有帮助的话)。
x = [mxn]
y = [m]
z=intersect(x,y)
例如
x = [1,2,3,4,5,]
y = [5,6,2]
z = intersect(x,y)
将会导致
z = [2,5]
改进的答案是:-
[z,ix,iy] = intersect(x,y)
其中 ix, iy 给出 y 元素在 X 中存在的行索引。