how can i average some numbers in one column when they are the same kind which described by another column

1 view (last 30 days)
I got a file called 2004.csv, which contains two columns Looks like follows: How can I get the mean of UA and US. I don't know how to separate according to the UA and US lable. I'm new to matlab, is there any easy solution to solve it?
  • Title Number
  • UA 2
  • UA 3
  • US 1
  • US 2
  • US 3
  • NW 0
  • NW 2

Accepted Answer

Star Strider
Star Strider on 28 Jan 2015
One way to do it:
M = {'UA' 2
'UA' 3
'US' 1
'US' 2
'US' 3
'NW' 0
'NW' 2};
[Mu, ia, ic] = unique(M(:,1));
MiUS = find(strcmpi(Mu,{'US'}));
MiUA = find(strcmpi(Mu,{'UA'}));
MeanUS = mean(cell2mat(M(MiUS == ic,2)))
MeanUA = mean(cell2mat(M(MiUA == ic,2)))
produces:
MeanUS =
2
MeanUA =
2.5

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!