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

1 vue (au cours des 30 derniers jours)
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!

Réponse acceptée

Stephan
Stephan le 26 Nov 2020
Modifié(e) : Stephan le 26 Nov 2020
ans = abc;
ans(abc(:,3)==0,:) = []

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by