MATLAB: Another Matrix Subtraction Problem

accumarraymatrix array

note: I asked a similar question earlier, but now I have a problem where the numbers in the first column do not start with 1 and followed by counting numbers (2, 3, 4..etc.)
I have this matrix:
67 0
67 2
67 3
78 3
78 4
90 1
90 6
In the second column, I need to find the highest and lowest number that are associated with the same number in the first column, and then subtract the two. In row 1, the numbers 67 and 0 exist. In row 3, the numbers 67 and 3 exists. I need to subtract the 3 and the 0 and get 3 as my output.
The output should be a matrix that looks like this:
67 3
78 1
90 5
Also if a number in the first column only occurs once
67 0
67 2
67 3
78 3
78 4
90 1
90 6
92 5
Then the number in the second column associated with it in the output matrix should be a 0.
67 3
78 1
90 5
92 0

Best Answer

As long as the first column are integers, this will work:
M = [ 67 0
67 2
67 3
78 3
78 4
90 1
90 6
92 5];
Mu = unique(M(:,1));
R = accumarray(Mu, [1:length(Mu)], [], @(x)[max(M(M(:,1)==Mu(x),2))-min(M(M(:,1)==Mu(x),2))]);
Result = [Mu R(Mu)]
Result =
67 3
78 1
90 5
92 0