Main Content

ismember

(Not Recommended) Dataset array elements that are members of set

The dataset data type is not recommended. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Description

LiA = ismember(A,B) for dataset arrays A and B returns a vector of logical values the same length as A. The output vector, LiA, has value 1 (true) in the elements that correspond to observations in A that are also present in B, and 0 (false) otherwise.

example

LiA = ismember(A,B,vars) returns a vector of logical values the same length as A. The output vector, LiA, has value 1 (true) in the elements that correspond to observations in A that are also present in B for the variables specified in vars only, and 0 (false) otherwise.

[LiA,LocB] = ismember(___) also returns a vector the same length as A containing the index to the first observation in B that corresponds to each observation in A, or 0 if there is no such observation. You can use any of the previous input arguments.

Examples

collapse all

Load sample data.

load('hospital')
B = hospital(1:50,1:5);

This set dataset array, B, has 50 observations on 5 variables.

Specify a query dataset array.

rng('default')
rIx = randsample(100,10);
A = hospital(rIx,1:5)
A = 
               LastName             Sex       Age    Weight    Smoker
    YLN-495    {'COLEMAN'  }        Male      39     188       false 
    LQW-768    {'TAYLOR'   }        Female    31     132       false 
    DGC-290    {'BUTLER'   }        Male      38     184       true  
    DAU-529    {'REED'     }        Male      50     186       true  
    REV-997    {'ALEXANDER'}        Male      25     171       true  
    QEQ-082    {'COX'      }        Female    28     111       false 
    AGR-528    {'SIMMONS'  }        Male      45     181       false 
    PUE-347    {'YOUNG'    }        Female    25     114       false 
    HVR-372    {'RUSSELL'  }        Male      44     188       true  
    XUE-826    {'JACKSON'  }        Male      25     174       false 

Check which observations in A are present in B.

LiA = ismember(A,B)
LiA = 10x1 logical array

   0
   1
   0
   0
   0
   0
   0
   1
   0
   1

Display the observations in A that are present in B.

A(LiA,:)
ans = 
               LastName           Sex       Age    Weight    Smoker
    LQW-768    {'TAYLOR' }        Female    31     132       false 
    PUE-347    {'YOUNG'  }        Female    25     114       false 
    XUE-826    {'JACKSON'}        Male      25     174       false 

Find the location of the observations in B.

[~,LocB] = ismember(A,B)
LocB = 10×1

     0
    10
     0
     0
     0
     0
     0
    28
     0
    13

Display the observations in B that match observations in A.

B(LocB(LocB>0),:)
ans = 
               LastName           Sex       Age    Weight    Smoker
    LQW-768    {'TAYLOR' }        Female    31     132       false 
    PUE-347    {'YOUNG'  }        Female    25     114       false 
    XUE-826    {'JACKSON'}        Male      25     174       false 

Input Arguments

collapse all

Query array, containing the observations to be found in B, specified as a dataset array.

Set array, specified as a dataset array. When an observation in A is found in B, for all variables or only those variables specified in vars, the corresponding element of LiA is 1.

Variables, specified as a string array or cell array of character vectors containing variable names, or a vector of integers containing variable column numbers. vars indicates which variables to match observations on in A and B.

Output Arguments

collapse all

Indicators whether observations from the query array A are members of the set array B, returned as a vector of logical values the same length as A. LiA has value 1 (true) when the corresponding observation in A is present in B. Otherwise, LiA has value 0 (false).

If you specify vars, LiA has value 1 when the corresponding observation in A is present in B for the variables in vars only.

Index to the first observation in B that corresponds to each observation in A, returned as a vector of the same length as A, for all variables or only those variables specified in vars.

Data Types: logical

Version History

Introduced in R2012b