How to code a generic for loop that removes a row with missing requirement
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to create a generic program using a for loop that removes the row if a user checks their number against it and it is not there. A matrix will have unspecified number of rows and columns, a user will input their value say n, if the user's n is not in row k of A, row k should be deleted.
A = magic(50);
row = size(A,1);
n = 3; %or whatever number
for r = 1:1:row
if not(ismember(n, A)) %if n is not a member of whatever row it is on, it should be deleted
%what goes in here?
end
end
댓글 수: 0
채택된 답변
Voss
2022년 4월 1일
A = magic(50);
row = size(A,1);
n = 3; %or whatever number
rows_to_delete = [];
for r = 1:1:row
if ~ismember(n, A(r,:)) %if n is not a member of whatever row it is on, it should be deleted
rows_to_delete(end+1) = r;
end
end
% delete the rows
A(rows_to_delete,:) = [];
댓글 수: 3
추가 답변 (0개)
참고 항목
카테고리
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!