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

1 次查看(过去 30 天)
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!

采纳的回答

Stephan
Stephan 2020-11-26
编辑:Stephan 2020-11-26
ans = abc;
ans(abc(:,3)==0,:) = []

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by