Delete values from different columns depending on a value from a specific column

I have a double variable with 3 columns. Sometimes, in the second column I obtain the value zero. In case it happens I want to completely erase that line. For instance:
12346 67 89
1245 0 765
56 0 99
19862 8 675
What I would like to get:
12346 67 89
19862 8 675
Thanks in advance for your help.

 採用された回答

Star Strider
Star Strider 2014 年 4 月 28 日
This works:
a = [12346 67 89; 1245 0 765; 56 0 99; 19862 8 675];
a((a(:,2) == 0),:) = [];
It uses ‘logical indexing’ to eliminate the entire row with a zero in the second column.

6 件のコメント

Maria
Maria 2014 年 4 月 28 日
Thank you very much. It worked perfectly. :)
Star Strider
Star Strider 2014 年 4 月 28 日
編集済み: Star Strider 2014 年 4 月 28 日
My pleasure!
The sincerest form of appreciation here on MATLAB Answers is to Accept the answer that solves your problem. It also helps others find it if they have similar questions.
Maria
Maria 2014 年 4 月 28 日
Done!
Star Strider
Star Strider 2014 年 4 月 28 日
Thanks!
Maria
Maria 2014 年 5 月 18 日
Do you know how can I do the same, but considering the fact that some elements of the cell array are words (strings)? Thank you!
Here you go:
MCA = {1 2 3; 4 'ab' 6; 7 8 'bc'; 4 3 8; 7 2 1; 'yz' 10 pi}; % Mixed Cell Array
FS = cellfun(@ischar,MCA); % Find string elements
SR = find(sum(FS,2)); % Find rows containing string elements
NSR = setdiff(1:size(MCA,1), SR)'; % Find rows NOT containing string elements
MCAnew = MCA(NSR,:) % Create new array
Have fun!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2014 年 4 月 28 日

コメント済み:

2014 年 5 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by