How to plot mean scores of dataset with different number of variables

4 views (last 30 days)
I have a matrix of 2922 samples with scores for 23 components (2922x23) double.
I want to plot the mean value of the first component for data points 1-320, 321-655, 656-1001, 1002-1338, 1339-1684, 1685-2027, 2028-2348, 2349,2581 and 2582-2922 including an error bar of +/-1 standard deviation for each group. The components are not numerical values.
What would be the simplest way to do this for each component? The number of data points within each set will never be the same. Ideally I would like to group the 9 "sets" into 2 groups of 4 and 5 which have different coloured plots.

Accepted Answer

the cyclist
the cyclist on 15 Nov 2019
% Make up some pretend data
data = rand(2922,23);
% 1-320, 321-655, 656-1001, 1002-1338, 1339-1684, 1685-2027, 2028-2348, 2349,2581 and 2582-2922
edge = [0 320 655 1001 1338 1684 2027 2348 2581 2922];
% Find the number of sections
numberSections = numel(edge)-1;
% Preallocate memory for mean and std dev
sectionMean = nan(numberSections,1);
sectionStd = nan(numberSections,1);
% Calculate the stats for each section
for ne = 1:numberSections
indexToSection = edge(ne)+1:edge(ne+1);
sectionMean(ne) = mean(data(indexToSection,1));
sectionStd(ne) = std(data(indexToSection,1));
end
% Plot
figure
errorbar(sectionMean,sectionStd)
  8 Comments
the cyclist
the cyclist on 20 Nov 2019
Edited: the cyclist on 20 Nov 2019
You definitely do not want just
std(ne)
because ne is a scalar, and std(ne) is going to be zero. It is in no way related to the standard deviation of your data.
This addition to my code above works for me:
% Make up some pretend data
data = rand(2922,23);
% 1-320, 321-655, 656-1001, 1002-1338, 1339-1684, 1685-2027, 2028-2348, 2349,2581 and 2582-2922
edge = [0 320 655 1001 1338 1684 2027 2348 2581 2922];
% Find the number of sections
numberSections = numel(edge)-1;
% Preallocate memory for mean and std dev
sectionMean = nan(numberSections,1);
sectionStd = nan(numberSections,1);
sectionStdErr = nan(numberSections,1);
% Calculate the stats for each section
for ne = 1:numberSections
indexToSection = edge(ne)+1:edge(ne+1);
sectionMean(ne) = mean(data(indexToSection,1));
sectionStd(ne) = std(data(indexToSection,1));
sectionStdErr(ne) = sectionStd(ne)/sqrt(numel(data(indexToSection,1)));
end
% Plot
figure
errorbar(sectionMean,sectionStdErr)
The modification is that I added the calculation of the standard error, and plotted it instead of the standard deviation.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!