No BSD License  

Highlights from
MINTERSECT -- Multiple set intersection.

5.0

5.0 | 4 ratings Rate this file 29 Downloads (last 30 days) File Size: 1.23 KB File ID: #6144

MINTERSECT -- Multiple set intersection.

by David Fass

 

29 Oct 2004 (Updated 01 Nov 2004)

A generalization of INTERSECT to handle multiple inputs.

| Watch this File

File Information
Description

MINTERSECT(A,B,C,...) when A,B,C... are vectors returns the values common to all A,B,C... The result will be sorted. A,B,C... can be cell arrays of strings.

MINTERSECT repeatedly evaluates INTERSECT on successive pairs of sets, which may not be very efficient. For a large number of sets, this should probably be reimplemented using some kind of tree algorithm.

MINTERSECT(A,B,'rows') when A,B,C... are matrices with the same number of columns returns the rows common to all A,B,C...

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
16 Jan 2008 James K

What 'intersect' should have been. Thanks to the author, very straightforward.

28 Jul 2008 Claas V

Useful extension of intersect.m

23 Aug 2009 Stratis Gavves

Very handy. Mathworks should already have this functionality in the intersect function.

19 Dec 2011 Alexander

Very useful function, thanks. Although it doesn't return indices like usual intersect.
That's actually quite easy. Here's what that function should look like. Please update your submition so other people could use it

function [runIntersect, varargout] = mintersect(varargin)
%MINTERSECT Multiple set intersection.
% MINTERSECT(A,B,C,...) when A,B,C... are vectors returns the values
% common to all A,B,C... The result will be sorted. A,B,C... can be cell
% arrays of strings.
%
% MINTERSECT repeatedly evaluates INTERSECT on successive pairs of sets,
% which may not be very efficient. For a large number of sets, this should
% probably be reimplemented using some kind of tree algorithm.
%
% MINTERSECT(A,B,'rows') when A,B,C... are matrices with the same
% number of columns returns the rows common to all A,B,C...
%
% [C,IA,IB,IC,...] = MINTERSECT(...) returns required number of indices, similar to INTERSECT
%
% See also INTERSECT

flag = 0;
if isempty(varargin),
    error('No inputs specified.')
else
    if isequal(varargin{end},'rows'),
        flag = 'rows';
        setArray = varargin(1:end-1);
    else
        setArray = varargin;
    end
end

nout = min(length(setArray),max(nargout,1)-1);
varargout = cell(nout, 1);
for k = 1:nout
    varargout(k) = {(1:length(setArray{k}))'};
end

runIntersect = setArray{1};
for i = 2:length(setArray),
    
    if isequal(flag,'rows'),
        [runIntersect, i1, i2] = intersect(runIntersect,setArray{i},'rows');
    elseif flag == 0,
        [runIntersect, i1, i2] = intersect(runIntersect,setArray{i});
    else
        error('Flag not set.')
    end
    for k = 2:min(i,nout+1)
        varargout(k-1) = {varargout{k-1}(i1)};
    end
    if k <= nout
        varargout(k) = {i2};
    end
    
    if isempty(runIntersect)
        for k = 1:nout
            varargout(k) = {};
        end
        return
    end
    
end

19 Dec 2011 Alexander

One small correction: at the end the line

varargout(k) = {};

should be changed to

varargout(k) = {[]};

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
intersect David Fass 22 Oct 2008 07:34:20
set David Fass 22 Oct 2008 07:34:20
intersection David Fass 22 Oct 2008 07:34:20
multiple David Fass 22 Oct 2008 07:34:20

Contact us at files@mathworks.com