Delete columns if sum of a group of 10 columns is zero

2 vues (au cours des 30 derniers jours)
Alessandra
Alessandra le 20 Mai 2023
Commenté : Alessandra le 25 Mai 2023
I need to delete a group of coluns if their sum is equal a zero. I have 132 columns and 39174 lines. My data start at 14 line and 3 column. So if the sum of the columns 3 to 12 = zero, all those columns should be removed. And the code should keep searching for the next group of 10 columns equal zero (columns 13 to 22, 23 to 32, ...). Can anybody please help me?
I tried:
colToRemove = sum(Data1(15:end,3,10,end))==0;
Data1(:,colToRemove) = [];

Réponse acceptée

the cyclist
the cyclist le 20 Mai 2023
Modifié(e) : the cyclist le 20 Mai 2023
Here is one way.
for nc = 123:-10:3
colIdx = nc:(nc+9);
if sum(Data1(15:end,colIdx),"all")==0
Data1(:,colIdx) = [];
end
end
I worked right-to-left to check the columns, otherwise the indexing is complicated by the fact that one has deleted columns.
Also, you said that your data start at row 14, but you checked rows 15 and onward, so I used 15 as well. You may need to change that.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by