0

I have a matrix, a small sample of the data:

A=

1 3 658

2 3 475

5 3 769

1 3 856

6 7 1579

2 3 678

5 3 118

6 7 617

So now, I want to find for every unique combination of column A and B the lowest value in column C, preferably in a new Matrix.

So the output will be:

B=

1 3 658

2 3 475

5 3 118

6 7 617

Could you point me in the direction of the best way to do this? Thanks in advance

Rogier
  • 65
  • 1
  • 6

2 Answers2

3

A combination of sortrows and unique with the rows option should give you the desired result.

A = sortrows(A); % After the sort unique combinations will be adjacent and with increasing values in 3rd column
[~,ia] = unique(A(:,1:2),'rows'); % Find index of all the unique comb in col 1 & 2, unique only returns the first index
B = A(ia,:); 
Some Guy
  • 1,787
  • 11
  • 15
1

If the values in the first two columns are positive integers and the values in the third are nonzero, you can also do it as follows:

[ii, jj, vv] = find(accumarray(A(:,[1 2]), A(:,3), [], @min, 0, true));
B = [ii jj vv];
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147