ismember

Class: dataset

Dataset array elements that are members of set

The dataset data type might be removed in a future release. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Syntax

LiA = ismember(A,B)
LiA = ismember(A,B,vars)
[LiA,LocB] = ismember(___)

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.

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.

Input Arguments

A

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

B

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

vars

Cell array of strings containing variable names or a vector of integers containing variable column numbers, indicating which variables to match observations on in A and B.

Output Arguments

LiA

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.

LocB

Vector the same length as A containing the index to the first observation in B that corresponds to each observation in A, for all variables or only those variables specified in vars.

Examples

expand all

Find Observations That Are Members of a Dataset Array

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 =

     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 =

     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 
Was this topic helpful?