From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: check if vector is subset of another
Date: Thu, 05 May 2011 18:44:25 -0700
Organization: NNTP Server
Lines: 69
Message-ID: <ipvjpq$cem$>
References: <ipvgg4$dur$>
Reply-To: <HIDDEN>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: Gecko/20110414 Thunderbird/3.1.10
X-Notice: Filtered by postfilter v. 0.8.2
Xref: comp.soft-sys.matlab:725419

On 5/5/2011 5:48 PM, Pr B wrote:
> i have vectors of doubles a = [1 2 3], b = [1 2 3 4], c = [1 3 2 4].
>i want to check if one vector is the perfect subset of another while preserving order.
>  e.g. i would like to see that a is a perfect subset of b but not a subset of c.
>i'm trying to use ismember to do this, but the output is just a
>sequence of [1 1 1] when i do isequal(a,a(ismember(a,b))).  any suggestions?

There might be a function in matlab to do this in shorter way,
but I would now do it this way (before coffee): to find if A is
subset of B, scan B looking at each segment of length(A), and
see if one gets a hit. Once I have coffee, may be I will do it
in another way.

Here is a quick hacK;

function r=isPerfectSubset(A,B)
scanIt2 = @(A,B) bsxfun(@eq,A,B);

scanIt1 = @(A,B) arrayfun(@(i) ...

r = any(cellfun(@all,scanIt1(A,B)));

Here is a test driver:

function testIt

a = [1 2 3];
b = [1 2 3 4];
c = [1 3 2 4];
d = [0 2 1 2 3 3];

if isPerfectSubset(a,b)
    disp('perfect subset');
    disp('not perfect subset');

if isPerfectSubset(a,c)
    disp('perfect subset');
    disp('not perfect subset');

if isPerfectSubset(a,d)
    disp('perfect subset');
    disp('not perfect subset');


Here is a run result:

EDU>> testIt

perfect subset
not perfect subset
perfect subset