フィルターのクリア

How to delete that row from the matrix which the summation of elements more than 5?

1 回表示 (過去 30 日間)
I have matrix :
C =[
1 0 0 0 0 6
1 0 0 0 5 6
1 0 0 4 0 6
1 0 0 4 5 6
1 0 3 0 0 6
1 0 3 0 5 6
1 0 3 4 0 6
1 0 3 4 5 6
1 2 0 0 0 6
1 2 0 0 5 6
1 2 0 4 0 6
1 2 0 4 5 6
1 2 3 0 0 6
1 2 3 0 5 6
1 2 3 4 0 6
1 2 3 4 5 6];
I need to remove that row, where the number of elements more than 5.
For example the last row of matrix c has 6 elements, so the code should remove the last row from matrix C.
I could write the code, but it is just for one row.
I need to make it suitable for all rows. I have tried, but I couldn't.
Could anyone help me?
My code:
C =[
1 0 0 0 0 6
1 0 0 0 5 6
1 0 0 4 0 6
1 0 0 4 5 6
1 0 3 0 0 6
1 0 3 0 5 6
1 0 3 4 0 6
1 0 3 4 5 6
1 2 0 0 0 6
1 2 0 0 5 6
1 2 0 4 0 6
1 2 0 4 5 6
1 2 3 0 0 6
1 2 3 0 5 6
1 2 3 4 0 6
1 2 3 4 5 6];
C~=0,2;
E=size(C,2);
if sum(C(16,1:E))>5 %<-------this just checking row-16
C(16,:)=[];<---------------and removing row 16
end
Here, in my code I wrote the number of single row manually, but actually right code should find automatically all rows.

採用された回答

the cyclist
the cyclist 2016 年 7 月 20 日
編集済み: the cyclist 2016 年 7 月 20 日
removeIdx = sum(C>0,2)>5;
C(removeIdx,:) = [];
  3 件のコメント
the cyclist
the cyclist 2016 年 7 月 20 日
編集済み: the cyclist 2016 年 7 月 20 日
Sorry, I misread your question. I thought you wanted the sum of elements to be less than or equal to 5.
Now I see you want the count of non-zero element to be less than or equal to 5. I have edited my code accordingly. Please try again.
Beibit Sautbek
Beibit Sautbek 2016 年 7 月 20 日
Thank you very much! It works, and I am so happy!

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

その他の回答 (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