Code covered by the BSD License  

Highlights from
multisetdiff

Be the first to rate this file! 2 Downloads (last 30 days) File Size: 2.57 KB File ID: #25267

multisetdiff

by Ben Petschel

 

09 Sep 2009 (Updated 14 Sep 2009)

like setdiff, but any repeated elements of A are removed only once for each time they occur in B

| Watch this File

File Information
Description

setdiff for multisets. multisetdiff(A,B) removes repeated elements of A only once for each time they occur in B.

Example:
multisetdiff([1,1,2,2],[1,1,2]) % returns 2

To work with cell arrays of strings or using the rows feature this version requires LEXCMP (see below). The current version does not work for non-string cell arrays (unless you have an implementation of SORT for cell arrays). Non-cell arrays can in principle be any objects for which SORT, SIGN, MINUS and comparison operations are defined.

See the help for more details.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
lexcmp

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
10 Sep 2009 Jos (10584)

Nice utility! Just a thought, for numerical vectors, this might be more convenient:

C = union(A,B) ;
res = C(histc(A,C) > histc(B,C))

To deal with rows of matrices or cell array strings, one could convert A and B into indices using, e.g., the third output of unique.

10 Sep 2009 Jos (10584)

Please ignore my previous thought as it is incorrect !!

Please login to add a comment or rating.
Updates
14 Sep 2009

added support for inf/nan values

Tag Activity for this File
Tag Applied By Date/Time
setdiff Ben Petschel 09 Sep 2009 10:58:08
multisetdiff Ben Petschel 09 Sep 2009 10:58:08
multiset Ben Petschel 09 Sep 2009 10:58:08

Contact us at files@mathworks.com