MATLAB: How to find the indices of same values within a vector and bin them together

bin same valuesfind indiceshistchistogramunique

I have two vectors with the same length (N). For example:
A=[ 1 2 3 3 3 3 4 5]; B=[ 9 8 7 6 5 6 7 8];
What I need to do is to take an average over elements in vector 'B' that their corresponding indices in vector 'A' have same values. In this example, element 3,4,5,6 in vector A are all equal to '3' so I need to take an average over (7+6+5+6/4) in vector B. I need to do this over a large data and bin them together. I tried different ways and it didn't work. How can I do this?

Best Answer

The following statement will return the list of all the averages according to the way you mentioned in your question.
meanVector = splitapply(@(x) mean(x), B', A')
since A have 5 unique elements, meanVector will also contain 5 elements corresponding to the mean value from B.