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')![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1069495/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1069495/image.jpeg)
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!