Create Categorical Arrays from Cell Arrays of Character Vectors
Gender and Location contain data that belong in categories. Each cell array contains character
vectors taken from a small set of unique values (indicating two genders and three locations
respectively). Convert Gender and Location to categorical arrays.
Gender = categorical(Gender);
Location = categorical(Location);
Search for Members of a Single Category
For categorical arrays, you can use the logical operators == and ~= to find the data that is in, or not
in, a particular category.
Determine if there are any patients observed at the location, 'Rampart General Hospital'.
any(Location=='Rampart General Hospital')
ans = logical
0
There are no patients observed at Rampart General Hospital.
Search for Members of a Group of Categories
You can use ismember to find data in a particular group of categories. Create a logical vector for the
patients observed at County General Hospital or VA Hospital.
VA_CountyGenIndex = ...
ismember(Location,{'County General Hospital','VA Hospital'});
VA_CountyGenIndex is a 100-by-1 logical array containing logical true (1) for each element in the
categorical array Location that is a member of the category County General Hospital or VA
Hospital. The output, VA_CountyGenIndex contains 76 nonzero elements.
Use the logical vector, VA_CountyGenIndex to select the LastName of the patients observed at
either County General Hospital or VA Hospital.
VA_CountyGenPatients = LastName(VA_CountyGenIndex);
VA_CountyGenPatients is a 76-by-1 cell array of character vectors.
Select Elements in a Particular Category to Plot
Use the summary function to print a summary containing the category names and the number of
elements in each category.
summary(Location)
County General Hospital
39
St. Mary's Medical Center
24
VA Hospital
37
Location is a 100-by-1 categorical array with three categories. County General Hospital
occurs in 39 elements, St. Mary s Medical Center in 24 elements, and VA Hospital in 37
elements.
Access Data Using Categorical Arrays
8-25
Précédent

- 329/1450

Suivant