Removing specific rows from a matrix where its corresponding element in another column is zero.

1 view (last 30 days)
Hi All,
I've a matrix abc, I want to remove those columns from it where its corresponding element in third column is zero.
For example , I should be getting the answer as:
ans =[ 1 2 3 ; 4 5 6; 1 5 8; 2 6 9 ; 3 5 9;]
Below is my attempt where I'm getting out of bound error for index position 4 which is obvious as the size is 8 x 3.
abc = [ 1 2 3 ; 4 5 6; 3 4 0 ; 1 5 8; 2 6 9 ; 3 5 9; 2 6 0; 7 7 0;]
sz = size(abc)
row =sz(1)
col = sz(2)
for ii = 1: row
for jj = 1:col
if abc(ii,jj) ==0
abc(:,jj) = [];
jj = jj+1
else
ii = ii+1
end
end
end
Please correct my code.
Thanks in advance!

Accepted Answer

Stephan
Stephan on 26 Nov 2020
Edited: Stephan on 26 Nov 2020
ans = abc;
ans(abc(:,3)==0,:) = []

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!