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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by