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

3 ビュー (過去 30 日間)
Kirsty Milligan
Kirsty Milligan 2019 年 11 月 15 日
コメント済み: Kirsty Milligan 2019 年 11 月 20 日
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.

採用された回答

the cyclist
the cyclist 2019 年 11 月 15 日
% 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 件のコメント
the cyclist
the cyclist 2019 年 11 月 20 日
編集済み: the cyclist 2019 年 11 月 20 日
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.
Kirsty Milligan
Kirsty Milligan 2019 年 11 月 20 日
Thank you so much! Works perfectly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by