So, presume a matrix like so:
20 2
20 2
30 2
30 1
40 1
40 1
I want to count the number of times 1 occurs for each unique value of column 1. I could do this the long way by [sum(x(1:2,2)==1)] for each value, but I think this would be the perfect use for the UNIQUE function. How could I fix it so that I could get an output like this:
20 0
30 1
40 2
Sorry if the solution seems obvious, my grasp of loops is very poor.
Indeed unique is a good option:
Taking apart that last line:
@(y)length(x(x(:,1)==y & x(:,2)==1))which finds the length of the portion of x where the conditionx(:,1)==y & x(:,2)==1)holds (called logical indexing). So for each of the unique elements, it finds the row in X where the first is the unique element, and the second is one.