MATLAB: Skip subs/index values with accumarray

accumarrayindexmeansubszero

I created an index (groupID) for a vector A. The vector is sorted from the smallest value to the biggest one. The function and the groupID look like:
[~,groupID]=histc(A,0:5:100);
groupID = [1 1 3 3 3 5 5];
In the next step I want to calculate the average for every groupID. I did this as follows:
groupMeans = accumarray(groupID,A,[],@mean);
My problem is that accumarray fills in the output array for every skipped groupID (because e.g. 2 and 4 are missing in the index) a zero. I dont want these zeros in my output array. Is there a solution for this? My Matlab version is R2012b

Best Answer

Use unique's third output to get the indices:
[uni,~,idx] = unique(groupID(:));
groupMeans = accumarray(idx,A,[],@mean);
[uni,groupMeans]