MATLAB: How to retain only the rows of a matrix based on the unique numbers in the first column of the matrix

keeping unique rows based on only the first column

Hello guys Say that a=[1 3 3; 1 4 3; 2 4 3; 2 5 2; 2 4 2; 3 2 1; 4 3 4; 4 3 2] I need to keep the rows where I have only unique numbers in the first column of the matrix. That means I want to have this matrix a= [1 3 3; 2 4 3; 3 2 1; 4 3 4]. Or I also need to get [1 4 3; 2 4 2; 3 2 1; 4 3 2]. That means retaining the last unique row (based on only the first column) instead of the first. Are they possible? please help.
Kind regards
Sayeed

Best Answer

[ii,jj,kk]=unique(a(:,1),'stable');
out1=a(jj,:)
out2=cell2mat(accumarray(kk,1:numel(kk),[],@(x) {a(max(x),:)}));