Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

the cyclist
the cyclist il 20 Lug 2016
Modificato: the cyclist il 20 Lug 2016
removeIdx = sum(C>0,2)>5;
C(removeIdx,:) = [];
  3 Commenti
the cyclist
the cyclist il 20 Lug 2016
Modificato: the cyclist il 20 Lug 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by