Seeing as the floodgates are opened, I might as well throw in my two cents.
A solution with a loop works well, but you can also eliminate loops, at the expense of readability. First, you can get the unique values in the last column with unique
:
stringKeys = unique(datABC(:,3))'
Then you can use an anonymous function and cellfun
to count the occurrences of each key:
memberFun = @(x) ismember(datABC(:,3),x);
keyOccurrences = cellfun(@(x) nnz(memberFun(x)),stringKeys)
To compute the mean of the corresponding data for each of the first two columns, you can again use cellfun
with non-uniform outputs:
colMeanFun = @(x) mean(reshape([datABC{memberFun(x),1:2}],[],2),1);
colMeans = cellfun(colMeanFun,stringKeys,'UniformOutput',false);
colMeans = vertcat(colMeans{:})
Also have a look ate strcmpmi
, which can be used in place of ismember
but will ignore case.
Test data:
datABC = {[45] [67] 'A'; [34] [44] 'A'; [11] [84] 'A'; ...
[23] [68] 'A'; [34] [44] 'B'; [30] [94] 'B'; ...
[304] [414] 'C'; [78] [110] 'C'; [34] [120] 'C'}; % 9-by-3