loop for removing highest value until specific value is reached
2 ビュー (過去 30 日間)
古いコメントを表示
for exmaple,
for 10x10 matrix A
i want to make loop which removes highest element until sum of A is less than 20
and i want to know each removed element's position
i think 'while-loop' is needed
( I only know finding the first highest value using 'for-loop')
0 件のコメント
採用された回答
Adam Danz
2022 年 7 月 18 日
A = randi(13,10);
while sum(A,'all','omitnan')>20
[~,idx] = max(A(:),[],'omitnan');
A(idx) = NaN;
end
isRemoved = isnan(A)
sum(A,'all','omitnan')
その他の回答 (1 件)
David Hill
2022 年 7 月 18 日
[b,idx]=sort(A(:));
IDX=idx(cumsum(b)>=20);%linear index of each of the removed element's position
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!