MATLAB: How to average column 3 when column 1 and column 2 satisfies some condition in matrix

conditional averagingmatrix manipulation

i have a n*3 matrix as shown below
A B C
1 1 2
1 2 22
1 2 12
1 3 0
1 3 4
2 2 6
2 2 0
2 3 2
2 3 2
I need the result as
A B C
1 1 2
1 2 17 # average
1 3 4 #no need to average if value =0
2 2 6
2 3 2

Best Answer

T = readtable('file1.txt');
or
M = [1 2 22
1 2 12
1 3 0
1 3 4
2 2 6
2 2 0
2 3 2
2 3 2];
T = array2table(M,'v',{'A','B','C'});
solution:
out = varfun(@(x)sum(x)/nnz(x),T,'GroupingVariables',{'A','B'});