Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
check if vector is subset of another

Subject: check if vector is subset of another

From: Pr B

Date: 6 May, 2011 00:48:05

Message: 1 of 10

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?

Subject: check if vector is subset of another

From: dpb

Date: 6 May, 2011 00:58:00

Message: 2 of 10

On 5/5/2011 7: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?

If length is same,

 > a=[1:4];
 >> b=randperm(4)
b =
      4 2 3 1
 >> all(a==b)
ans =
      0
 >> all(a==a)
ans =
      1

There's bound to be something clever for the subset case but it doesn't
hit me just now other than the possible number of subsets of the shorter
in the other as a loop.

--

Subject: check if vector is subset of another

From: Darren Rowland

Date: 6 May, 2011 00:59:07

Message: 3 of 10


The file findsubmat by Matt Fig should do the job
http://www.mathworks.com/matlabcentral/fileexchange/23998-findsubmat

Hth
Darren

Subject: check if vector is subset of another

From: Nasser M. Abbasi

Date: 6 May, 2011 01:44:25

Message: 4 of 10

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

Subject: check if vector is subset of another

From: Nasser M. Abbasi

Date: 6 May, 2011 01:56:09

Message: 5 of 10


I detected a minor end condition bug.

Below is version 2, fixing the end condition. Please Ignore last post.

%------------------------
%version 2
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)+1,'UniformOutput',false);

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

------------------------------------

-----------------------------
function testIt

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

checkIt(isPerfectSubset(a,b));
checkIt(isPerfectSubset(a,c));
checkIt(isPerfectSubset(a,d));

     function checkIt(r)
        if r
           disp('perfect subset');
        else
           disp('not perfect subset');
        end
     end

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


EDU>> testIt
perfect subset
not perfect subset
perfect subset

--Nasser

Subject: check if vector is subset of another

From: Roger Stafford

Date: 6 May, 2011 02:22:04

Message: 6 of 10

"Pr B" <pb2297@columbia.edu> wrote in message <ipvgg4$dur$1@newscl01ah.mathworks.com>...
> ....... i would like to see that a is a perfect subset of b but not a subset of c. ......
- - - - - - - - - - -
t = isempty(setdiff(a,b)) & ~isempty(setdiff(a,c));

Roger Stafford

Subject: check if vector is subset of another

From: Nasser M. Abbasi

Date: 6 May, 2011 02:32:08

Message: 7 of 10

On 5/5/2011 7:22 PM, Roger Stafford wrote:
> "Pr B"<pb2297@columbia.edu> wrote in message<ipvgg4$dur$1@newscl01ah.mathworks.com>...
>> ....... i would like to see that a is a perfect subset of b but not a subset of c. ......
> - - - - - - - - - - -
> t = isempty(setdiff(a,b))& ~isempty(setdiff(a,c));
>
> Roger Stafford

Roger,

The above does not really work. The order is not persevered in
these operations. OP wanted the order of the sequence 'a'
preserved.

--Nasser

Subject: check if vector is subset of another

From: Pr B

Date: 6 May, 2011 02:37:04

Message: 8 of 10

funny, i didn't anticipate this to be that tough of a problem. thanks for everyone's input thus far.

Subject: check if vector is subset of another

From: Nasser M. Abbasi

Date: 6 May, 2011 02:41:16

Message: 9 of 10

On 5/5/2011 7:37 PM, Pr B wrote:
> funny, i didn't anticipate this to be that tough of a problem. thanks for everyone's input thus far.

It is only a little tricky since you wanted the order of 'a' persevered. Else it is trivial.

--Nasser

Subject: check if vector is subset of another

From: Pr B

Date: 6 May, 2011 02:48:04

Message: 10 of 10

now in actuality, i want to test all possible pairs of [a b c d] to see if any of them are subsets of each other...what part of this code do i modify?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us