2010-04-18 221 views
1
>> XOR(X,X) 
??? Undefined function or method 'XOR' for input arguments of type 'logical'. 

爲什麼XOR不能用於邏輯矩陣?如何使XOR在MATLAB中用於邏輯矩陣?

我嘗試了更簡單的例子:

>> A=[1 0;1 0]; 
>> B=[1 1;0 0]; 

>> XOR(A,B) 
??? Undefined function or method 'XOR' for input arguments of type 'double'. 

我如何正確使用XOR

回答

1

如何如下:

 
C = abs(A-B); 

上面的語句使C A和B的XOR,因爲XOR是如此,其中的條目是彼此不同,和1-0或0-1將給1或-1(而abs的那個會給1),而0-0和1-1都是1.

如果你確實想要,你可以創建一個帶有以下定義的「XOR.m」文件:

 
function C=XOR(A,B) 
% function C=XOR(A,B) 
% INPUTS: 
%  A - m x n matrix, consisting only of 1s or 0s. 
%  B - m x n matrix, consisting only of 1s or 0s. 
% OUTPUT: 
%  C - m x n matrix, containing the logical XOR of the elements of A and B 
C=abs(A-B) 

但是,喲你應該記住,Matlab中的函數調用非常緩慢,所以你可能只想寫出我給你的定義,無論你在哪裏需要它。

編輯
我最初沒有理解你的問題....你需要使用xor而不是XOR,如果是抱怨,你的矩陣是雙打,而不是邏輯值,然後使用A==1B==1代替ABMatlab is case sensitive when it comes to variable names and built-in functions such as the xor function

+0

不,你根本沒有使用'XOR'。 – Gtker 2010-04-18 11:26:09

+0

@Runner,對不起,你最初並不清楚你正在嘗試使用:http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xor.html – 2010-04-18 11:33:58

+0

@Runner,我相信我有現在回答你的問題。 – 2010-04-18 11:34:18

8

它適用於我。

A=[1 0;1 0]; 
B=[1 1;0 0]; 

xor(A,B) 
ans = 
    0  1 
    1  0 

然而,當我嘗試這個...

XOR(A,B) 
??? Undefined function or method 'XOR' for input arguments of type 'double'. 

看到區別。保留大寫字母以解決問題。

我認爲模糊是由於他們的文檔中使用了MathWorks公約。當他們在幫助中顯示功能的名稱時,他們使用全部大寫。例如,這裏是xor的幫助。

>> help xor 
XOR Logical EXCLUSIVE OR. 
    XOR(S,T) is the logical symmetric difference of elements S and T. 
    The result is logical 1 (TRUE) where either S or T, but not both, is 
    nonzero. The result is logical 0 (FALSE) where S and T are both zero 
    or nonzero. S and T must have the same dimensions (or one can be a 
    scalar). 

即使如此,當您使用函數時,您在函數名稱中使用小寫字母。