矩阵行大小与向量项比较?

问题描述 投票:0回答:2

我有矩阵 [m x n] 和向量 [m],我想将每一行与相应的向量数进行比较,有没有办法实现这种向量化方法?

matlab octave
2个回答
6
投票

使用

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
中列出。


0
投票

您可以尝试使用

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 中存在的行索引。

© www.soinside.com 2019 - 2024. All rights reserved.