MATLAB: Assigning an index to each row based on column values in a matrix.

indexMATLABmatrix manipulationrow-column comparision

Hi, I have a matrix like this.
a= [ 1 2 8
1 2 2
1 3 1
1 2 5
2 4 3
3 5 4
4 6 2
4 7 1
4 6 5
4 6 3
5 7 3
6 8 3
6 9 1
6 9 1
6 9 5
7 6 4
7 8 5
9 8 2]
I want to assign an index to each row based on the following criterion – If the value in the first column and second column is same, I want to compare the third column value and assign an index 1 if it is the minimum among all rows with the same first and second column values and 0 otherwise. So for example, comparing first and second rows, they have the same corresponding first and second column values and second row has the minimum third column value among this. So the program should assign 0 for the first row and 1 for the second row.
Also, If all the three column values are same, I want to assign 1 to any one row and 0 to all other such rows.
So the ideal index vector I want for this is as follows
[0
1
1
0
1
1
1
1
0
0
1
1
1
0
0
1
1
1]
I wrote the following code to do this, but I am unable to get the required output.
minindex = ones(1,size(a,1))
for j=1:size(a,1)
for i = 1:size(a,1)
if ((a(j,1) == a(i,1)) && (a(j,2) == a(i,2)))
if (j~=i)
minindex(j) = (a(i,3) > a(j,3));
end
end
end
end
minindex'
Can any one help me on how to achieve this?
Thanks in advance!

Best Answer

[As,I] = sortrows(a);
As = any([1 1;diff(As(:,1:2))],2);
I = As(I)
.
.
EDIT The above will not work always, but this should.
[As,I] = sortrows(a);
As = any([1 1;diff(As(:,1:2))],2);
Z = false(size(As,1),1);
Z(I(As)) = true