How can I find MIN value with same number in the other column?

2 ビュー (過去 30 日間)
Sungwoo Park
Sungwoo Park 2016 年 10 月 17 日
コメント済み: Sungwoo Park 2016 年 10 月 17 日
Hi, This one is really tricky.
I have a matrix like below:
A=[
1,1;
1,2;
1,3;
2,4;
2,5;
2,6;
3,7;
3,8;
3,9;
2,10;
2,11;
2,12;
1,13;
1,14;
1,15;]
I want to fine Min of each (1,2,3,2,1) groups. Therefore, the answer I want to get is, [
1,1;
2,4;
3,7;
2,10;
1,13]
I tried using unique function but I couldn't solve problem because there are 2 separate groups of 1 and 2.
Please help me and thank you very much!

採用された回答

Guillaume
Guillaume 2016 年 10 月 17 日
編集済み: Guillaume 2016 年 10 月 17 日
Here is one way of doing it:
A=[1,1;
1,2;
1,3;
2,4;
2,5;
2,6;
3,7;
3,8;
3,9;
2,10;
2,11;
2,12;
1,13;
1,14;
1,15;]
runlengths = diff([0; find(diff(A(:, 1))); size(A, 1)]); %find length of each run of consecutive numbers
rowids = repelem((1:numel(runlengths))', runlengths); %assign unique ids to each run
[A(cumsum(runlengths), 1), accumarray(rowids, A(:, 2), [], @min)] %use accumarray to find the minimum per rowid

その他の回答 (2 件)

Tony Castillo
Tony Castillo 2016 年 10 月 17 日
I think you should try it on this way A2= min (A)

Andrei Bobrov
Andrei Bobrov 2016 年 10 月 17 日
ii = diff([~A(1);A(:,1)])~=0;
out = [A(ii,1), accumarray(cumsum(ii),A(:,2),[],@min)];

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by