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

5 views (last 30 days)
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 Comment
Image Analyst
Image Analyst on 21 Feb 2015
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()?

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 21 Feb 2015
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])]

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!