MATLAB: Removing all rows with duplicate information in two columns

duplicateremove rows

Hi there,
I have a large matrix with 5 columns. As an example ,imagine a=[1 2 3 4 5; 1 3 4 5 6; 1 2 3 5 5] is just a small sample of the larger matrix. I am trying to remove the rows where columns 1 and columns 5 match. So for my example matrix a, I would need to remove BOTH rows 1 and 3 since they have the same values in column 1 AND column 5. I know that the unique function can get me to the point where one of the rows is removed but I need both of them removed. I have been at this all day and am stumped (and it is probably a simple answer!)
Any help is much appreciated!

Best Answer

Try this:
a=[1 2 3 4 5; 1 3 4 5 6; 1 2 3 5 5];
[~,~,idx] = unique(a(:,[1 5]),'rows');
tally = accumarray(idx,(1:numel(idx)).',[],@(x){idx(x)});
notsame = a(cellfun(@(x)numel(x)==1,tally),:)
producing:
notsame =
1 3 4 5 6
I also tried it on a larger version. It took a bit of time for me to prove that this works with the larger matrix.