File Exchange

image thumbnail

MINTERSECT -- Multiple set intersection.

version 1.0 (1.23 KB) by

A generalization of INTERSECT to handle multiple inputs.

4.91667
12 Ratings

32 Downloads

Updated

No License

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...

Comments and Ratings (14)

Hesham Eraqi

Hesham Eraqi (view profile)

Does the job :)

Edwin Khoo

Ravi Pandit

Very useful :) Thanks

Lukas Pop

Nice function!

Yu Li

Yu Li (view profile)

Seema Chopra

I am using MATLAB Version: 8.4.0.150421 (R2014b, but I don't see MINTERSECT function. Can you please confirm, if there is an alternative for this to handle more than 2 arrays.

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

Hiren

Hiren (view profile)

Works great - thanks!

David

David (view profile)

Excellent function David, thanks. Good extension Alexander

One small correction: at the end the line

varargout(k) = {};

should be changed to

varargout(k) = {[]};

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

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

Claas V

Useful extension of intersect.m

James K

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

MATLAB Release
MATLAB 6.5 (R13)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video