MATLAB: How to average certain elements in a matrix using for loop.

matrix manipulation

Hello,
I am trying to operate on a matrix that has the following columns:
M=[x_coordinates y_coordinates ID] I would like to find the average x and y coordinates for same ID, in other words to group all the points that has the same ID. Can you please advice me on how to do it with Matlab.
Best regards, Jack

Best Answer

A=[ 1 2 10;5 8 10;4 3 20]
c1=A(:,1);
c2=A(:,2);
[ii,jj,kk]=unique(A(:,3));
cc1=[ii accumarray(kk,c1,[],@mean)]
cc2=[ii accumarray(kk,c2,[],@mean)]