I want to check that the contents of two arrays are equal, ignoring ordering of the elements.
I couldn't find an existing answer on this for some reason. There are two parts to this question, depending on whether the arrays are sets or not.
1. Checking equality for two sets
Assuming there are no duplicates I could use set operaions but it doesn't look like Matlab has sets, only set operations, and I couldn't find a setisequal function. Also, there are so many possible ways to do it I'm not sure which is the best:
assert(isequal(intersect(A, B), union(A, B)))
assert(isempty(setdiff(A,B)) & isempty(setdiff(B,A)))
assert(all(ismember(B, A)) & all(ismember(A, B)))
However, if there may be different numbers of duplicates in either of the two arrays, then the above set operations will ignore them.
2. Checking equality for non-sets
This is the best I could think of in this case but it involves two sort operations which is quite inefficient:
No doubt there is something very simple that I am missing here...