MATLAB: Calculate mean of columns for a unique value of column variable

mean of columns for unique value in 1st row.

I have a matrix of 38767*31. I want to calculate mean of columns for unique value of variable in columns (in first row).Let me clarify my problem.
Let
A=[1 2 1 2 1 1 2 1 1
3 2 1 2 3 4 5 6 2
2 1 2 1 3 5 4 2 6]
for unique value for 1st row of A, I have to calculate mean of all corresponding columns. I mean I should get as follows,
B=[1 2; 3.66 3; 3.33 2];
for unique of 1st row (=1), mean of corresponding values (3+1+3+4+6+1)/6=3.166 for 2nd row and (2+2+3+5+2+6)/3=3.33 for 3rd row. for value of 1st row (=2), mean of corresponding values (2+2+5)/3=3 for 2nd row and (1+1+4)/3=2 for 3rd row.Hope I am understood. I tried using accumarray but not able to do. my code is,
A=importdata('abc.dat');
y=accumarray(unique(A(1,:)),A(1,:),[],@mean));
It shows following error, Second input VAL must be a vector with one element for each row in SUBS, or a scalar.? Help. Thanks in advance.

Best Answer

This will do what you want with a for loop:
A = [1 2 1 2 1 1 2 1 1
3 2 1 2 3 4 5 6 2
2 1 2 1 3 5 4 2 6];
[uniqueA1,~,k] = unique(A(1,:));
numberUniqueA1 = numel(uniqueA1);
meanArray = zeros(size(A,1),numberUniqueA1);
for nu = 1:numberUniqueA1
indexToThisUniqueValue = (nu==k)';
meanArray(:,nu) = mean(A(:,indexToThisUniqueValue),2);
end