No BSD License  

5.0

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

MINTERSECT -- Multiple set intersection.

by

 

29 Oct 2004 (Updated )

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   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
27 Aug 2014 Robert Daly

The extension with indices in the post by Alexander was exactly what I was looking for. Worked just like intersect but with 3 vectors

25 Oct 2012 Hiren

Works great - thanks!

07 Aug 2012 David

Excellent function David, thanks. Good extension Alexander

19 Dec 2011 Alexander

One small correction: at the end the line

varargout(k) = {};

should be changed to

varargout(k) = {[]};

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

23 Aug 2009 Stratis Gavves

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

28 Jul 2008 Claas V

Useful extension of intersect.m

16 Jan 2008 James K

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

Contact us