MATLAB: How to perform calculations on common elements of an array

common values of an arraymeanStatistics and Machine Learning Toolbox

I have A_names =[a,a,a,b,b,c,c,c,c]; B_values = [1,2,3,2,2,3,4,4,4]; length(A_names) = length(B_values). I need to find average of B_values for the common elements of A_names

Best Answer

If you have the Statistics and Machine Learning Toolbox, use grpstats() - that's what it's meant for - to get statistics by group.
A_names = {'a';'a';'a';'b';'b';'c';'c';'c';'c'}
B_values = [1,2,3,2,2,3,4,4,4]
t = table(A_names, B_values')
output = grpstats(t, 1)