- 2列目のデータが連続で並んでいる
- 1列目のデータが昇順になっている
配列要素内の最大値だけを抽出する方法
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
翔 池田
am 25 Apr. 2021
Kommentiert: Akira Agata
am 26 Apr. 2021
ZZ =
1 10
1 13
2 13
1 14
2 14
3 14
3 15
3 16
2 17
2 18
上記の配列で,
以下のように2列目の値が同じ値の中で,1列目の値が最大の行だけ抽出する方法を教えてください.
尚,ループなしの方法でお願いいたします.
ans =
1 10
2 13
3 14
3 15
3 16
2 17
2 18
0 Kommentare
Akzeptierte Antwort
Hernia Baby
am 25 Apr. 2021
ZZのような行列であること前提で話します。
前提
やりかた
2列目にunique関数を設けて、最後の行のみを抽出します。
そしてidxに当てはまる行を抽出します。
[c,idx] = unique(ZZ(:,2),'last');
ZZ(idx,:)
ans =
1 10
2 13
3 14
3 15
3 16
2 17
2 18
ーーーーーーーーーーーー
unique関数についてはこちら
1 Kommentar
Akira Agata
am 26 Apr. 2021
[group, gID] = findgroups(ZZ(:,2));
maxVal = splitapply(@max,ZZ(:,1),group);
result = [maxVal, gID];
>> result
result =
1 10
2 13
3 14
3 15
3 16
2 17
2 18
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!