Standard deviation, Mean values for each class

조회 수: 4 (최근 30일)
Ivan Mich
Ivan Mich 2020년 3월 24일
댓글: Adam Danz 2020년 3월 25일
Hello, I have a problem with a code.
First of all I have a .csv file with 2 columns, as I show you in the Image. in the first column the data are split by 0.5 (1,1.5,2).
I would like to find mean values and standard deviation for each of one class, I mean one value of standard deviation and mean value for the data with 1 in , one value of standard deviation for the 1.5 and one value of standard deviation for the 2.
I mean I wanna use the errorbar command.
I think I should use a loop who reads 1st column one by one but I can not make it.
Could anyone help me?

채택된 답변

Adam Danz
Adam Danz 2020년 3월 24일
편집: Adam Danz 2020년 3월 24일
Use grpstats() if you have the Stats & Machine Learning Toolbox.
data = [ 1 1 1.5 1.5 2 2; 10 9 7 6 3.5 8]',;
[dataMean, dataStd] = grpstats(data(:,2), data(:,1), {'mean', 'std'})
If your data are in table format,
Tstats = grpstats(T, 'data1', {'mean','std'});
% ^^^^^ VariableName for column 1
If you don't have Stats & Machine Learning Toolbox,
stats = splitapply(@(x)[mean(x), std(x)], data(:,2), findgroups(data(:,1)));
  댓글 수: 8
Ivan Mich
Ivan Mich 2020년 3월 25일
편집: Adam Danz 2020년 3월 25일
well, I send you the full code in order to see it and tell me where I am wrong:
clc
clear
filename1= 'e.xlsx'
[d1,tex]= xlsread(filename1);
x=d1(:,1);
y=d1(:,2);
stats = splitapply(@(y)[mean(y), std(y)], y, findgroups(x));
meanvalues=stats(:,1)
stdvalues=stats(:,2)
err=errorbar(meanvalues,stdvalues)
% lower error line
hold on
plot(x,y,'bo')
hold on
plot(x, y-err)
hold on
% upper error line
plot(x, y+err)
Command window send me the message :
Undefined operator '-' for input arguments of type 'matlab.graphics.chart.primitive.ErrorBar'.
Error in eo (line 24)
Adam Danz
Adam Danz 2020년 3월 25일
plot(x, y-err)
plot(x, y+err)
1) You should be using the meanvalues insetad of x.
2) err, in your code, is the handle to the error bars. You should be using the stdvals.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by