2

I want to convert Matlab code which is doing an XOR on two vectors into Python. I have tried to do this using the numpy.logical_xor() function but this is failing because the two arrays being compared are not of the same shape, preventing broadcasting from working.

The Matlab code I'm trying to emulate:

test5=setxor(1:length(test2(test3)),test4);

The current (non-working) attempt at the above in Python:

test5 = np.logical_xor(np.array(range(len(test2[test3]))), test4)

When this line executes I get the following error:

ValueError: operands could not be broadcast together with shapes (366217,) (120655,)

I also get the same result when I add an axis to each of the arrays using numpy.expand_dims(), I get messages such as

ValueError: operands could not be broadcast together with shapes (1, 366217) (1, 120655)
ValueError: operands could not be broadcast together with shapes (366217,1) (120655,1)

The issue is the different lengths of test2[test3] and test4, and it seems that the Matlab setxor() functino works fine on vectors of different lengths but the numpy equivalent requires vectors of equal length.

How can I perform an XOR on two 1-D numpy arrays of different lengths? Or perhaps I am misunderstanding what is happening in the Matlab code and/or using the wrong Python function for this?

1
  • It might be easier to help if you gave a simple example of desired input and output (with test2, test3, and test4 of much smaller sizes), and explain in words what the Matlab code is meant to do.
    – DSM
    Oct 10, 2015 at 16:16

1 Answer 1

3

MATLAB/Octave setxor

Return the elements exclusive to A or B, sorted in ascending order.

that's a set operation

octave:2> setxor([1,2,3,4],[5,3])
ans =
   1   2   4   5

np.logical_xor is an element by element comparison, not a set operation.

I think there are some set operations in numpy, but I'd have look them up. I know there is a set class in Python

In [176]: x=set([1,2,3,4])    
In [177]: x.symmetric_difference([5,3])
Out[177]: set([1, 2, 4, 5])

setdiff1d is a set difference function, which could be used as

In [188]: xa=np.array([1,2,3,4])
In [189]: ya=np.array([5,3])
In [190]: np.concatenate([np.setdiff1d(xa,ya),np.setdiff1d(ya,xa)])
Out[190]: array([1, 2, 4, 5])

It uses np.unique and np.in1d; a setxor could be rewritten using those functions.

In [199]: np.concatenate([xa[np.in1d(xa,ya,invert=True)],
      ya[np.in1d(ya,xa,invert=True)]])
Out[199]: array([1, 2, 4, 5])

(may want to use xa=np.unique(xa) etc first).

My guess is that if there is a defined setxor function it will be built from these same pieces.


Bingo, a Google search on numpy set operations produced:

http://docs.scipy.org/doc/numpy/reference/routines.set.html

In [201]: np.setxor1d(xa,ya)
Out[201]: array([1, 2, 4, 5])

It does: (for 2 unique arrays)

    aux = np.concatenate( (ar1, ar2) )
    aux.sort()
    flag = np.concatenate( ([True], aux[1:] != aux[:-1], [True] ) )
    flag2 = flag[1:] == flag[:-1]
    return aux[flag2]

So it makes a sorted concatenation of the arrays, and then removes the elements that aren't unique.

1
  • Excellent answer, this is exactly the solution, it works for me now. Thanks! Oct 10, 2015 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.