Delete rows only if there are at least a repeated zero value in the previous or next row of the same column

1 回表示 (過去 30 日間)
I need your help fellows. I want to remove all rows which contain at least a repeated zero value in the previous/next row of the second column. Look at the next example:
Input=
124.2 8.6 7.2 -4.8
131.1 1.8 1.4 -1.2
131.9 0.0 0.0 0.0
123.0 0.0 0.0 0.0
2323.0 3.0 3.0 3.0
2323.0 0.0 0.0 0.0
221.0 3.1 4.0 5.6
57.0 1.0 231.0 122.0
987.0 0.0 0.0 0.0
4454.0 0.0 0.0 0.0
3.0 0.0 0.0 0.0
434.0 0.0 0.0 0.0
Output=
124.2 8.6 7.2 -4.8
131.1 1.8 1.4 -1.2
2323.0 3.0 3.0 3.0
2323.0 0.0 0.0 0.0
221.0 3.1 4.0 5.6
57.0 1.0 231.0 122.0
Only it was maintened the sixth row of the original table (input) due to there was not a zero in the previous/next row of the same column (second column).
Thanks in advance for your help!
  3 件のコメント
Miguel L
Miguel L 2016 年 9 月 25 日
Walter thanks for your comments. Let me clarify the issue: It is necessary remove rows looking for zeros at the previous/next row of the second column. At the end, I am looking for the result enlisted at the output:
Output=
124.2 8.6 7.2 -4.8
131.1 1.8 1.4 -1.2
2323.0 3.0 3.0 3.0
2323.0 0.0 0.0 0.0
221.0 3.1 4.0 5.6
57.0 1.0 231.0 122.0
Walter Roberson
Walter Roberson 2016 年 9 月 25 日
Your line
131.1 1.8 1.4 -1.2
is followed by a line that has 0 in its second column, so according to your rules it should be removed.
Perhaps you want the rule to be that if there is a set of rows in which there are at least two rows in a row with 0 in the second column, that the entire block with zeros there should be removed ?

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 9 月 25 日
Assuming that the rule is that if there is a set of rows in which there are at least two rows in a row with 0 in the second column, that the entire block with zeros there should be removed, then:
mask1 = input(:,2) ~= 0;
mask2 = mask1(1:end-1) | mask1(2:end);
keep_row = [true;mask2] & [mask2;true];
output = input(keep_row, :);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimscape Electrical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by