필터 지우기
필터 지우기

How to create code to remove rows from a table

조회 수: 1 (최근 30일)
DIMITRA
DIMITRA 2019년 4월 10일
댓글: DIMITRA 2019년 4월 10일
Hello everyone! I need to remove every row that contains a specific number (let's say 0) from a 2D Table.
How can i create a code PLEASE?
  댓글 수: 2
Stephen23
Stephen23 2019년 4월 10일
@DIMITRA: what have you tried so far?
DIMITRA
DIMITRA 2019년 4월 10일
@Stephen Cobeldick First of all thank you very much for the answer!
My table's (DIRP) size is (478,354).
I began with turning those rows entirely to 0.
for i=1:478
for j=1:354
if DIRP(i,j)==0
DIRP(i,:) =0; %if i type DIRP(i,:) =[]; to delete the row, my %table changes size so i tried this first
end
end
end

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

채택된 답변

Bob Thompson
Bob Thompson 2019년 4월 10일
You're on the right track by using logic to identify whether a row contains a 0, but you would be better off marking different rows for zeros, and then removing them all at the end.
check = zeros(478,354); % Create an array to look for checks
rows = []; % Empty array to name rows to remove
for i=1:478
for j=1:354
if DIRP(i,j)==0
check(i,j) =1; % Mark elements as zero
end
end
if sum(check(i,:))>0; % Check row for zeros
rows = [rows; i];
end
end
DIRP = DIRP(~rows,:); % Remove flagged rows
There are a few alternative options to this. The first is to catch the entire logic array at the beginning and then use one loop to go through all rows.
check = DIRP == 0; % Logic array, should be same end result of previous check
rows = [];
for i = 1:478;
if sum(check(i,:))>0; % Check row for zeros
rows = [rows; i];
end
end
DIRP = DIRP(~rows,:); % Remove flagged rows
Alternatively, you can index your output to remove certain rows based on the logic check all at once. It's ultimately doing the same thing as the above codes, just in one line.
reduced = DIRP(DIRP~=any(DIRP==0,2));
The any command searches all of the specified range (set to 'rows' with the '2'). Setting the range of DIRP~=any... means that you are looking for rows which do not contain a 0.
  댓글 수: 1
DIMITRA
DIMITRA 2019년 4월 10일
Problem solved, you've been very helpfull really.
Thank you very much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by