How to plot Box Plots of two arrays?

1 view (last 30 days)
Delany MacDonald
Delany MacDonald on 6 Apr 2016
Answered: BhaTTa on 10 Sep 2024
I am given an array called useAalarm that is a 21 x 144 array. They represent 21 days of 144 people. I had to find the number of female and male cohorts in the array. When I did so I found the sum of the useAlarm for females to be 1 x 74 and the sum of useAlarm to be 1 x 70. How do i got about plotting box plots for the two arrays when the arrays are of different size.

Answers (1)

BhaTTa
BhaTTa on 10 Sep 2024
@Delany MacDonald, to create box plots for two arrays of different sizes in MATLAB, you can simply concatenate the arrays into a single vector and use a grouping variable to distinguish between the two groups (males and females).
Refer to the code below:
% Create dummy data: 21 days, 144 people
useAlarm = randi([0, 1], 21, 144); % Random 0s and 1s
% Assume first 74 are females and next 70 are males
femaleIndices = 1:74;
maleIndices = 75:144;
% Sum alarm usage over 21 days for each person
alarmFemales = sum(useAlarm(:, femaleIndices), 1);
alarmMales = sum(useAlarm(:, maleIndices), 1);
% Concatenate the data
alarmData = [alarmFemales, alarmMales];
% Create a grouping variable
genderLabels = [repmat({'Female'}, 1, length(alarmFemales)), repmat({'Male'}, 1, length(alarmMales))];
% Plot the boxplot
figure;
boxplot(alarmData, genderLabels);
ylabel('Total Alarm Usage');
title('Comparison of Alarm Usage by Gender');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!