This might be what you are after if you want to get the means for each unique value of v
in the order in which the unique values appear in v
:
>> [unv,iunv,iv] = unique(v);
>> umeans = accumarray(iv(:),u,[],@mean);
>> [~,ivorder] = sort(iunv);
>> umeans = umeans(ivorder)
umeans =
5.6000
18.0000
0.2333
If you want the means in order of the sorted values of v
, then just use the output of accumarray
without the reordering commands:
>> [unv,iunv,iv] = unique(v);
>> umeans = accumarray(iv(:),u,[],@mean)
umeans =
0.2333
5.6000
18.0000
Just ensure that u
is a row vector.