MATLAB: Find unique sets of values in matrix, eliminate duplications

matrix decimation unique sets of values

I had a lot of trouble wording this Q succinctly and accurately. I have large matrices with a fixed # of rows but varying #s of columns. I want to identify which columns have identical sets of values in a small subset of rows, then eliminate all but one of the like columns using a criteria applied to values in another row, not part of the aforementioned subset [of rows.] For example, I have a matrix with 20 rows and N columns (20 x N). I want to identify the unique combinations of values in, say, rows 5 & 6, then save only the column that has the maximum value in row 3 from the subset having identical values in rows 5 & 6.
A=[8 7 4 8 4 2 1 9 8 6
9 0 4 3 8 8 10 6 4 3
1 9 8 5 6 3 0 4 2 7
9 9 8 7 6 5 8 5 4 2
6 7 2 9 9 2 8 4 1 7
1 8 5 10 3 6 9 1 1 8
3 7 5 6 8 3 1 2 9 4
6 4 6 1 8 7 4 1 10 6
10 7 7 2 4 7 3 2 6 8
10 2 8 3 6 8 8 2 1 1
2 7 3 8 1 5 4 4 2 9
10 0 7 3 1 1 9 1 4 8
10 3 7 8 5 2 2 9 8 5
5 1 2 3 8 9 3 9 0 4
8 1 1 9 9 2 2 5 1 5
2 8 5 4 1 8 1 5 2 3
4 7 10 2 6 5 9 3 7 5
9 3 3 3 5 10 6 9 7 5
8 10 6 6 0 1 6 4 7 8
10 0 2 5 3 4 2 1 5 8];
In the above example, columns 2 & 10 have the same pair of values in rows 5 & 6: (7 & 8). I then want to eliminate the column(s) with the smaller value for row 3. In this case column 2 has the value 9 & column 10 has the value 7 so, I want to set A = A(:,1:9) or A(:,10) = []. I have tried using the unique function to identify pairs (sets, in general) of identical values [after transposing the matrix so I can work on rows] but I must not be using it properly. I assume I will use sortrows to sort the subsets in descending order (assuming my criteria is to save only the column with the max value in another row) – either before or after identifying like columns – and drop all but the max value-column.

Best Answer

r = [3;5;6];
B = A(r,:)';
[~,~,c] = unique(B(:,2:3),'rows','stable');
ii = find(histcounts(c,1:max(c)+1) > 1);
[lo,jj] = ismember(c,ii);
A(:,cell2mat(accumarray(jj(lo),find(lo),[],@(x){x(min(B(x,1))==B(x,1))}))) = [];