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!