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

multisetdiff

by Ben Petschel

 

09 Sep 2009 (Updated 14 Sep 2009)

Code covered by BSD License  

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

Download Now | 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)
Zip File Content  
Other Files license.txt,
multisetdiff.m
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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com