MATLAB: Remove column if value is greater than previous

for same values of row1 and row 2keep maximum row 3

Hi, I am quite new to matlab and i am in need of some help.
Lets say I have a matrix: [2 2 2 3 3 3 3 3 3 3 5; 6 6 7 2 2 2 2 2 2 2 6; 2 1 3 1 2 3 2 4 1 6 1]
My goal is to shrink the matrix to: [2 2 3 5; 6 7 2 6; 2 3 6 1]
I am looking for a code where I want to keep the maximum values for row three, where row1 and row2 have the same values.
My code should look something like this:
for count:1:end
if matrix(1,count)==matrix(1,count-1) && matrix(1,count)==matrix(1,count-1) && matrix(3,count-1)<matrix(3,count)
then remove previous column.
Code isn't finished, but this was just to give an rough idea.
The problem is that the for loop doesn't seem to work when i remove a column (because the column counter continues counting), and also when it removes the previous column there is no reference to compare to anymore.
The matrix above is an example, I thought about a for-loop, because the actual matrix has over 20000 columns.
Could somebody give me a some help me with the code?
Thanks in advance, Paul

Best Answer

For the 5-row case, this might be what you want:
load A
x=A(1:2,:).';
y=A(3:5,:).';
M=size(x,1);
[u,~,subs]=unique(x,'rows','stable');
ycell=accumarray(subs,(1:M).',[],@(k) {y(k,:)});
for i=1:numel(ycell)
[~,j]=max(ycell{i}(:,3));
ycell{i}=ycell{i}(j,:);
end
result=[u, cell2mat(ycell)].';