MATLAB: Understanding the syntax meaning behind (:,)

syntax

Hey I am new to matlab and trying to do some math calculation to find the centroid of a cluster of data, however, I am running into syntax confusion. Can someone explain the (:,) part of this code to me, in both uses if possible. what exactly does it mean?
c(:, j) = mean(d(:, find(ind == j)), 2);

Best Answer

The colon : as a subscript simply means "all of the values" for this subscript. So e.g. c(:,j) is all the rows of the j'th column (i.e., the entire j'th column), and c(i,:) would be all of the columns of the i'th row (i.e., the entire i'th row). So the syntax
c(:,j) = etc
sets the j'th column of c to whatever is on the right hand side.
The syntax
d(:, find(ind == j))
is all of the rows of d that correspond to the column numbers that result from the find(ind==j). If only one ind equals j, then this will result in one column of d. If multiple values in ind equal j, then this will result in multiple columns of d. The last part of the syntax
mean(etc,2)
simply takes the mean of the first argument across the 2nd dimension. I.e., this averages the columns of the first argument.
So, taken as a whole, this syntax
c(:, j) = mean(d(:, find(ind == j)), 2);
will take the average of all of the columns of d where ind equals j, and assign that to the j'th column of c.