Equality test not working?

3 visualizzazioni (ultimi 30 giorni)
Lauri
Lauri il 22 Dic 2011
Could someone please explain me this:
>> % uniqueness for the sets DOES NOT WORK! oh why?
iseq=isequal(unique([set01,set02]),[set01,set02])
diff=setdiff([set01,set02],unique([set01,set02]))
diff2=setdiff(unique([set01,set02]),[set01,set02])
--
iseq = 0
diff = Empty matrix: 1-by-0
diff2 = Empty matrix: 1-by-0
>>

Risposte (3)

Andrew Newell
Andrew Newell il 22 Dic 2011
The documentation says "c = setdiff(A, B) returns the values in A that are not in B. " The function unique extracts one copy of each element, so each element in [set01,set02] is also in unique([set01,set02]) and vice versa. The only difference is how many times they are repeated.
EDIT: This implements what you are trying to do:
s = [set01,set02];
[u, m] = unique(s);
notm = setdiff(1:length(s),m);
diff = s(notm)

Jan
Jan il 22 Dic 2011
The unique function sorts its input data. Therefore if "[set01, set02]" is unsorted, it does not equal "unique([set01, set02])", although both contain the same elements.
Try this:
iseq = isequal(unique([set01,set02]), sort([set01,set02]))
Another reason can be repeated elements in set01 and/or set02.

Lauri
Lauri il 22 Dic 2011
Thank you.
Yes, I forgot to add that only for one set the uniqueness resulted true. So indeed it was the sorting I didn't count in.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by