tags:

views:

293

answers:

3
>> XOR(X,X)
??? Undefined function or method 'XOR' for input arguments of type 'logical'.

Why XOR can't be used for logical matrix?

And I tried a more simple example:

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

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

How can I properly use XOR?

+1  A: 

How about the following:

C = abs(A-B);

The statement above makes C the XOR of A and B, because xor is true where the entries are different from each other, and 1-0 or 0-1 will give 1 or -1 (and abs of that will give 1), while 0-0 and 1-1 are both 1.

If you really want, you can create an "XOR.m" file with the following definition:

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)

However, you should keep in mind that function calls in Matlab are horrifically slow, so you might want to just write out the definition that I gave you wherever you happen to need it.

Edit
I did not originally understand your question.... you need to use xor and not XOR, and if it is complaining that your matrices are doubles instead of logicals, then use A==1 and B==1 instead of A and B. Matlab is case sensitive when it comes to variable names and built-in functions such as the xor function.

Michael Aaron Safyan
No,you didn't use `XOR` at all.
Gtker
@Runner, sorry, it wasn't originally clear that you were trying to use: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xor.html
Michael Aaron Safyan
@Runner, I believe I have now answered your question.
Michael Aaron Safyan
A: 

See this post. C = A~=B

Mikhail
+7  A: 

It works for me.

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

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

Yet when I try this...

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

See the difference. Leave caps off to fix the problem.

I think the ambiguity arises because of a MathWorks convention used in their documentation. When they show the name of a function in their help, they use all caps. For example, here is the help for 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).

Even so, when you use the function, you do so with lower case letters in the function name.

woodchips

related questions