How to find mean&stdev of different unique IDs in given table

2 次查看(过去 30 天)
Hi,
I want to calculate mean and stdv of the below table based on column1 (the column1 data may vary)
Now, I have the following different IDs in column1:HTD1,HTD2,HTD4,HTD6,HTD9,HTD12,HTD13 (because I knew now, but they may vary). Please someone kindly help how can I find the mean &stdev of different unique IDs in column1. And how can ensure which result is for which unique ID(like here: HTD1,HTD2,HTD4,HTD6,HTD9,HTD12,HTD13).
HTD1 Recipe1 0.1
HTD2 Recipe11 0.8
HTD1 Recipe2 1.1
HTD4 Recipe25 0.9
HTD2 Recipe22 2.0
HTD6 Recipe43 2.4
HTD1 Recipe8 1.4
HTD6 Recipe44 2.0
HTD9 Recipe56 0.9
HTD2 Recipe19 0.7
HTD1 Recipe8 1.5
HTD12 Recipe15 0.6
HTD13 Recipe91 1.1
HTD2 Recipe11 2.2
HTD1 Recipe2 1.2
HTD4 Recipe25 1.4
HTD6 Recipe44 0.1
HTD9 Recipe56 0.3
HTD2 Recipe19 1.4
HTD1 Recipe8 1.2
HTD12 Recipe15 0.6
HTD6 Recipe44 0.1
HTD6 Recipe44 0.1
HTD2 Recipe4 0.6
Many many thanks in advance.
  1 个评论
Image Analyst
Image Analyst 2015-2-21
Make it easy for us to help you and give the code where you created the table. And, have you looked at unique() and ismember()?

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2015-2-21
Since what you've shown is not how matlab displays a http://www.mathworks.com/help/releases/R2014b/matlab/tables.html table>, I assume what you've got is not truly a table but most likely a cell array. As per Image Analyst comment, it's always a good idea to show how you create your data (or use proper matlab syntax).
So, given:
c = {'HTD1' 'Recipe1' 0.1
'HTD2' 'Recipe11' 0.8
'HTD1' 'Recipe2' 1.1
'HTD4' 'Recipe25' 0.9
'HTD2' 'Recipe22' 2.0
'HTD6' 'Recipe43' 2.4
'HTD1' 'Recipe8' 1.4
'HTD6' 'Recipe44' 2.0
'HTD9' 'Recipe56' 0.9
'HTD2' 'Recipe19' 0.7
'HTD1' 'Recipe8' 1.5
'HTD12' 'Recipe15' 0.6
'HTD13' 'Recipe91' 1.1
'HTD2' 'Recipe11' 2.2
'HTD1' 'Recipe2' 1.2
'HTD4' 'Recipe25' 1.4
'HTD6' 'Recipe44' 0.1
'HTD9' 'Recipe56' 0.3
'HTD2' 'Recipe19' 1.4
'HTD1' 'Recipe8' 1.2
'HTD12' 'Recipe15' 0.6
'HTD6' 'Recipe44' 0.1
'HTD6' 'Recipe44' 0.1
'HTD2' 'Recipe4' 0.6}
Use unique to get the unique IDs and their location:
[ids, ~, rows] = unique(c(:, 1));
Use accumarray with these rows to calculate the standard deviation and mean of column 3:
means = accumarray(rows, cell2mat(c(:, 3)), [], @mean);
stddevs = accumarray(rows, cell2mat(c(:, 3)), [], @std);
output = [ids num2cell([means stddevs])]

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by