Get mean and stdev from all values
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I have a list like below. in the first column I have values from 0 to 91 (there can miss values).
0 3
0 4.50000000000000
0 1.37500000000000
0 5
0 3
0 0.838961038961039
0 2.80000000000000
1 1.50000000000000
1 0.555555555555556
1 1.71428571428571
1 3.38666666666667
1 4
1 3.38666666666667
2 1.73684210526316
2 1
2 1.30769230769231
2 2.73333333333333
2 1
2 2
I want to calculate the mean and standard deviation for all of these values if they exist.
How can I realise this? Now I have the folowing:
clear all; clc; close all;
standaard = readmatrix('/Users/diontheunissen/Documents/Apployee/Smart_driver/avgFuel.xlsx');
indices = find(standaard(:,1)==0);
standaard(indices,:) = [];
indices = find(standaard(:,2)>8);
standaard(indices,:) = [];
standaard(:,1) = round(standaard(:,1));
standaard = sortrows(standaard,1);
c = {};
for i = 0:5:85
if i == 0
lower = find(standaard(:,1) == i);
n = i+5;
upper = find(standaard(:,1) == n);
elseif i>1
lower = find(standaard(:,1) == i+1);
n = i+5;
upper = find(standaard(:,1) == n);
end
tab = standaard(lower(1):upper(end),:);
c{i+1} = tab;
end
referent2 = []
for j = 1:5:86
data = cell2mat(c(j));
referent(1,1) = data(1,1);
referent(1,2) = mean(data(:,2));
referent(1,3) = std(data(:,2));
referent2 = [referent2;referent]
end
snelheid = linspace(min(referent2(:,1)),max(referent2(:,1)));
a1 = interp1(referent2(:,1), referent2(:,2:end), snelheid, 'makima');
verbruik = a1(:,1);
upper = a1(:,1)+a1(:,2);
lower = a1(:,1)-a1(:,2);
figure
hold on
plot(snelheid, verbruik, '-b');
plot(snelheid, upper,'-r');
plot(snelheid, lower,'-g');
How can i change this script that i get the mean and stdev from all existing values?
thanks
댓글 수: 2
DGM
2021년 6월 3일
Can you clarify the structure of the file and what you're trying to do? I'm assuming you're trying to find the blockwise mean and std for column 2, the blocks being described by column 1. Are all blocks the same size? Are the values in column 1 sorted?
채택된 답변
DGM
2021년 6월 3일
편집: DGM
2021년 6월 3일
If column 1 is sorted:
% build test array
idx = repelem(1:10,randi(6,10,1));
idx = idx(1:20);
D = [idx.' rand(20,1)]; % the test array
% find length of blocks
blocklengths = diff([0 find(diff(idx)) numel(idx)]);
% split and process
C = mat2cell(D(:,2),blocklengths,1);
bkmean = cellfun(@mean,C)
bkstd = cellfun(@std,C)
댓글 수: 0
추가 답변 (1개)
Stephan
2021년 6월 3일
편집: Stephan
2021년 6월 3일
Use a table and then use findgroups combined with splitappy. This will allow you to solve the problem with a few lines of code. This will also work if the columns are not sorted.
댓글 수: 1
Steven Lord
2021년 6월 3일
findgroups can operate on numeric data as well as tabular data.
But rather than call those two functions sequentially, I'd probably just use groupsummary.
X = [0 3;
0 4.50000000000000;
0 1.37500000000000;
0 5;
0 3;
0 0.838961038961039;
0 2.80000000000000;
1 1.50000000000000;
1 0.555555555555556;
1 1.71428571428571;
1 3.38666666666667;
1 4;
1 3.38666666666667;
2 1.73684210526316;
2 1;
2 1.30769230769231;
2 2.73333333333333;
2 1;
2 2];
[M, BG] = groupsummary(X(:, 2), X(:, 1), @mean)
check = mean(X(X(:, 1)== 1, 2)) % This is the same as M(BG == 1)
S = groupsummary(X(:, 2), X(:, 1), @std)
MS = groupsummary(X(:, 2), X(:, 1), {@mean, @std}) % Compute M and S simultaneously
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!