MATLAB: How to find the average of certain rows in a column depending on the value of another column

averagelogical conditionsmatrix manipulation

I have a matrix with several columns and rows. I need to find the average of particular rows in one column depending on if the value in the row for another column is 1 or 2. So for example, in the following matrix:
1 1
2 1
3 2
4 2
5 1
I need to calculate the average of 1, 2 and 5 giving me 3.67. If possible, I also need to check the value in each row for an additional column as well.
Thank you so much!

Best Answer

Loop is a waste of time, try this:
splitapply(@mean,A(:,1),findgroups(A(:,2)))