Delete columns if sum of a group of 10 columns is zero
1 view (last 30 days)
Show older comments
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) = [];
0 Comments
Accepted Answer
the cyclist
on 20 May 2023
Edited: the cyclist
on 20 May 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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!