www.gusucode.com > stats 源码程序 matlab案例代码 > stats/SelectSubsetsofObservationsExample.m

    %% Select Subsets of Observations  
% This example shows how to select an observation or subset of observations
% from a dataset array.   

% Copyright 2015 The MathWorks, Inc.


%% Load sample data. 
% Load the sample dataset array, |hospital|. Dataset arrays can have observation
% (row) names. This array has observation names corresponding to unique
% patient identifiers. 
load hospital
hospital.Properties.ObsNames(1:10) 

%%
% These are the first 10 observation names.  

%% Index an observation by name. 
% You can use the observation names to index into the dataset array. For
% example, extract the last name, sex, and age for the patient with identifier
% |XLK-030|. 
hospital('XLK-030',{'LastName','Sex','Age'})  

%% Index a subset of observations by number. 
% Create a new dataset array containing the first 50 patients. 
ds50 = hospital(1:50,:);
size(ds50)  

%% Search observations using a logical condition. 
% Create a new dataset array containing only male patients. To find the
% male patients, use a logical condition to search the variable containing
% gender information. 
dsMale = hospital(hospital.Sex=='Male',:);
dsMale(1:10,{'LastName','Sex'})  

%% Search observations using multiple conditions. 
% You can use multiple conditions to search the dataset array. For example,
% create a new dataset array containing only female patients older than 40. 
dsFemale = hospital(hospital.Sex=='Female' & hospital.Age > 40,:);
dsFemale(1:10,{'LastName','Sex','Age'})  

%% Select a random subset of observations. 
% Create a new dataset array containing a random subset of 20 patients from
% the dataset array |hospital|. 
rng('default') % For reproducibility
dsRandom = hospital(randsample(length(hospital),20),:);
dsRandom.Properties.ObsNames  

%% Delete observations by name. 
% Delete the data for the patient with observation name |HVR-372|. 
hospital('HVR-372',:) = [];
size(hospital) 

%%
% The dataset array has one less observation.