Updating for loop boundaries in MATLAB

조회 수: 4 (최근 30일)
center
center 2020년 5월 27일
댓글: center 2020년 5월 30일
Hey, I have a for loop inside of that loop, there is a for loop whose upper boundary is row length of a matrix A, inside this loop I am reaching and deleting rows of A within some condition. The problem is, as far as I have reduced row length of A, my for loop stops because its boundary exceeds the reduced row length of A. In first for loop, I am performing some expressions to updating my A matrix meaning that, I made rows of A deletable within the condition that I referred. My second for loop's boundary should be updated in every deleting operation to be able to delete itself in second loop, enventually. How can I solve this problem? The structure of my code is more or less like below:
for c = 1:10
; %statements mading rows of A deletable
for i = 1:size(A,1)
if %conditions
A(i,:) = []; %deleting rows of A, so its row length reduced
else
; %do nothing
end
end
end

채택된 답변

Matt J
Matt J 2020년 5월 27일
for i = 1:size(A,1)
  댓글 수: 5
Matt J
Matt J 2020년 5월 29일
Using false() reflects the assumption that no rows will be deleted, until it is established in the loop that a delete condition for certain rows is satisfied. But you could have done it in other ways, e.g.,
keep=true(size(A,1),1); %assume all rows will be kept (not deleted)
for i = 1:size(A,1)
if %conditions
keep(i)=false;
end
end
A=A(keep,:);
center
center 2020년 5월 30일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by