Equality test not working?

3 ビュー (過去 30 日間)
Lauri
Lauri 2011 年 12 月 22 日
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
>>

回答 (3 件)

Andrew Newell
Andrew Newell 2011 年 12 月 22 日
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 2011 年 12 月 22 日
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 2011 年 12 月 22 日
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.

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by