Why does setdiff answer depend on order of arguments?

2 ビュー (過去 30 日間)
Brian Wilson
Brian Wilson 2019 年 9 月 9 日
編集済み: Stephen23 2019 年 9 月 10 日
>>bob = {'a','b','c'};
>>bill = {'a','b','c', 'd','e'}
As expected,
>>A = setdiff(bill,bob)
A =
'd' 'e'
BUT
>> B = setdiff(bob,bill)
B =
Empty cell array: 1-by-0
WHY??

採用された回答

Steven Lord
Steven Lord 2019 年 9 月 9 日
Were you expecting the output to contain elements that are only in one of the inputs but not both? That's not what setdiff does. If that is what you want to do, use setxor instead.
  2 件のコメント
Brian Wilson
Brian Wilson 2019 年 9 月 10 日
Thanks, yes. i was expecting it to tell me the "difference between the sets" i.e., which elements are not in both , but i see now, its computing A \ B.
Stephen23
Stephen23 2019 年 9 月 10 日
編集済み: Stephen23 2019 年 9 月 10 日
It is worth nothing that MATLAB setdiff follows the standard mathematical definition of "set difference", which is defined as A\B (i.e the elements of A that are not in B):
etc.
The setdiff documentation states "setdiff(A,B) returns the data in A that is not in B"
Although this is the accepted mathematical definition, the term "set difference" is rather ambiguous in common english. It would be nice if it used terms whose meaning was obvious.

サインインしてコメントする。

その他の回答 (2 件)

madhan ravi
madhan ravi 2019 年 9 月 9 日
編集済み: madhan ravi 2019 年 9 月 9 日
bob not in bill (nothing unique all elements of bob belong to bill)
help setdiff
  1 件のコメント
Brian Wilson
Brian Wilson 2019 年 9 月 10 日
Thanks madhan, I was confusing the functionality with setxor.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2019 年 9 月 9 日
setdiff implements set subtraction A \ B which is not commutative
  1 件のコメント
Brian Wilson
Brian Wilson 2019 年 9 月 10 日
thanks Walter, I was expecting it to do setxor.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by