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

조회 수: 9 (최근 30일)
Mekala balaji
Mekala balaji 2015년 2월 21일
답변: Guillaume 2015년 2월 21일
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에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by