MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
The findgroups function returns G, a vector of group numbers created from Smoker.
The splitapply function uses G to split Weight into two groups. splitapply applies
the mean function to each group and concatenates the mean weights into a vector.

findgroups returns a vector of group identifiers as the second output argument. The
group identifiers are logical values because Smoker contains logical values. The patients
in the first group are nonsmokers, and the patients in the second group are smokers.

smoker

smoker = 2x1 logical array

0
1

Split the patient weights by both gender and status as a smoker and calculate the mean
weights.

G = findgroups(Gender,Smoker);
meanWeight = splitapply(@mean,Weight,G)

meanWeight = 4×1

130.3250
130.9231
180.0385
181.1429

The unique combinations across Gender and Smoker identify four groups of patients:
female nonsmokers, female smokers, male nonsmokers, and male smokers. Summarize
the four groups and their mean weights in a table.

[G,gender,smoker] = findgroups(Gender,Smoker);
T = table(gender,smoker,meanWeight)

T=4×3 table
gender smoker meanWeight
______ ______ __________

Female false 130.32
Female true 130.92
Male false 180.04

9 Tables

Free download pdf