remove rows by pick the min value in another column

Can you help?
On below matrix, for same X and Y, pick smallest Z, then put into a new matrix by removing the rows? and also give the row number for which row is kept.
X Y Z
60,-40,1.1
55,-40,3.1
55,-45,4.5
60,-45,5.3
60,-40,7
55,-40,8.4
55,-45,9.8
60,-40,11.2
60,-45,12.6
55,-40,14
55,-45,15.4
60,-45,16.8
60,-40,18.2
60,-45,19.6
55,-40,21
60,-40,22.4
55,-45,23.8
60,-45,25.2
55,-40,26.6
55,-45,28

댓글 수: 5

I can think of at least two ways. How many can you think of?
And Jos thought of another the first time you asked this question. What was wrong with his answer?
booterr
booterr 2016년 4월 15일
편집: booterr 2016년 4월 15일
can you also give the row number for which row is kept?
Output the row number (or row index)which is kept. For example, there is a total of 20 rows in the question. Finally, only row 1,2,3, and 4 is kept. Can you output this row number?
not a duplicate questions. For the first thread, the answer did not give which row number the Z is located in the initial matrix.
I am not only needing the value of Z, but also need the row number of which Z is outputted.
thanks

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 4월 15일
편집: Andrei Bobrov 2016년 4월 15일
x=[55,-45,9.8
60,-40,11.2
60,-45,12.6
55,-40,14
55,-45,15.4
60,-45,16.8
60,-40,18.2
60,-45,19.6
55,-40,21
60,-40,22.4
55,-45,23.8
60,-45,25.2
55,-40,26.6
55,-45,28]
[a,~,c] = unique(x(:,1:2),'rows');
n = size(a,1);
valuemin = zeros(n,4);
for ii = 1:n
i0 = find(c == ii);
[valuemin(ii,3),i1] = min(x(i0,end));
valuemin(ii,[1:2,4]) = [a(ii,:),i0(i1)];
end
or
[a,~,c] = unique(x(:,1:2),'rows');
idx = accumarray(c,(1:size(x,1))',[],@(y)y(min(x(y,3))==x(y,3)));
valuemin = [a,x(idx,3),idx];

추가 답변 (0개)

제품

질문:

2016년 4월 14일

댓글:

2016년 4월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by