Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

the cyclist
the cyclist am 20 Jul. 2016
Bearbeitet: the cyclist am 20 Jul. 2016
removeIdx = sum(C>0,2)>5;
C(removeIdx,:) = [];
  3 Kommentare
the cyclist
the cyclist am 20 Jul. 2016
Bearbeitet: the cyclist am 20 Jul. 2016
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by