MATLAB: How to create code to remove rows from a table

delete rows

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?

Best Answer

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.