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

5 visualizaciones (últimos 30 días)
Alessandra
Alessandra el 20 de Mayo de 2023
Comentada: Alessandra el 25 de Mayo de 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) = [];

Respuesta aceptada

the cyclist
the cyclist el 20 de Mayo de 2023
Editada: the cyclist el 20 de Mayo de 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.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by