필터 지우기
필터 지우기

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 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