Path: news.mathworks.com!newsfeed-00.mathworks.com!kanaga.switch.ch!news-zh.switch.ch!switch.ch!feeder.news-service.com!94.75.214.39.MISMATCH!aioe.org!.POSTED!not-for-mail
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: Aioe.org NNTP Server
Lines: 69
Message-ID: <ipvjpq$cem$1@speranza.aioe.org>
References: <ipvgg4$dur$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: tUYQ4Ty9mMw9Pdc8TJRFQA.user.speranza.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
X-Notice: Filtered by postfilter v. 0.8.2
Xref: news.mathworks.com 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) ...
   scanIt2(A,B(i:length(A)+i-1)),...
   1:length(B)-length(A),'UniformOutput',false);

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

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');
else
    disp('not perfect subset');
end

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


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

end
--------------------------------

Here is a run result:

EDU>> testIt

perfect subset
not perfect subset
perfect subset


--Nasser