How to delete max values for every 5 rows

1 回表示 (過去 30 日間)
abdullah al-dulaimi
abdullah al-dulaimi 2022 年 12 月 29 日
編集済み: Voss 2022 年 12 月 29 日
I want to delete the max value for column 1 for every 5 rows with idx column 2
The matrix
A =
1 1
2 2
6 3
12 2
4 5
9 6
14 1
5 1
6 9
8 10
11 11
12 3
9 13
4 6
10 15
the result will be
1 1
2 2
6 3
4 5
9 6
5 1
6 9
8 10
11 11
9 13
4 6
10 15

回答 (1 件)

Voss
Voss 2022 年 12 月 29 日
編集済み: Voss 2022 年 12 月 29 日
A = [
1 1
2 2
6 3
12 2
4 5
9 6
14 1
5 1
6 9
8 10
11 11
12 3
9 13
4 6
10 15];
% group size
n = 5;
% index of max value, within each group of n (5)
[~,idx] = max(reshape(A(:,1),n,[]),[],1)
idx = 1×3
4 2 2
% convert to index in A
rows_to_delete = (0:numel(idx)-1)*n+idx
rows_to_delete = 1×3
4 7 12
% delete the rows of A
A(rows_to_delete,:) = []
A = 12×2
1 1 2 2 6 3 4 5 9 6 5 1 6 9 8 10 11 11 9 13

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by