如何比较 2 个相等长度的向量 - 我想获得不同的元素数量(在两个向量中具有相同的位置)。
Example:
x=[1 0 0 1 1]
y=[1 0 1 1 0]
result should be 2 since 3rd and 5th element of both vectors differ
一种可能的解决方案:
x==y将返回一个长度向量length(x)(或length(y)因为x和y长度相同),其中包含1wherex(i)==y(i)和0where x(i)~=y(i):
>> x==y
ans =
1 1 0 1 0
所以你需要做的就是将元素相加x==y并减去length(x)
>> length(x)-sum(x==y)
ans = 2
阿尔诺
将两个矩阵(/vectors)与 -
z = eq(x, y) % returns 1 for match and 0 for mismatch
它返回一个由 0 和 1 组成的矩阵z。最后计算其中的零个数:
sum(z == 0); % find total non matching elements
sum(ne(x, y)) % 找到所有不同的元素给出 2