1
如何比較2個相等長度的向量 - 我想獲得不同的元素數目(它們在兩個向量中具有相同的位置)。Octave - 比較載體(逐個元素)
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
如何比較2個相等長度的向量 - 我想獲得不同的元素數目(它們在兩個向量中具有相同的位置)。Octave - 比較載體(逐個元素)
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
具有相同的長度)與1
其中x(i)==y(i)
和0
其中x(i)~=y(i)
:
>> x==y
ans =
1 1 0 1 0
所以你需要做的就是將x==y
和su btract,要length(x)
>> length(x)-sum(x==y)
ans = 2
阿爾諾
爲了避免在矩陣的情況下的問題,我會建議更換'length'和'sum'用'numel'和'nnz'。我特別不喜歡「長度」,因爲它具有最大尺寸長度的奇怪定義。 – carandraug 2013-05-01 09:18:57
好點,儘管OP確實將'x'和'y'指定爲向量,而不是矩陣。 – am304 2013-05-01 09:28:12