ismember - Test for membership

Class

@categorical

Syntax

I = ismember(A,levels)
[I,IDX] = ismember(A,levels)

Description

I = ismember(A,levels) returns a logical array I the same size as the categorical array A. I is true (1) where the corresponding element of A is one of the levels specified by the labels in the categorical array, cell array of strings, or two-dimensional character array levels. I is false (0) otherwise.

[I,IDX] = ismember(A,levels) also returns an array of indices IDX containing the highest absolute index in levels for each element in A whose level is a member of levels, and 0 if there is no such index.

Examples

Example 1

For nominal data:

load hospital
sex = hospital.Sex; % Nominal
smokers = hospital.Smoker; % Logical
I = ismember(sex(smokers),'Female');
I(1:5)
ans =
     0
     1
     0
     0
     0

The use of ismember above is equivalent to:

I = (sex(smokers) == 'Female');

Example 2

For ordinal data:

load hospital
edges = 0:10:100;
labels = strcat(num2str((0:10:90)','%d'),{'s'});
AgeGroup = ordinal(hospital.Age,labels,[],edges);
I = ismember(AgeGroup(1:5),{'20s','30s'})
I =
     1
     0
     1
     0
     0

See Also

islevel, isundefined

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS