Table, delete columns with zero

5 次查看(过去 30 天)
I have a table with many rows and columns. How can I delete a column if any row contains zero? (I found a million ways to delete rows, but not columns, when dealing with tables.)

采纳的回答

newbie9
newbie9 2019-8-27
编辑:newbie9 2019-8-27
Got it; it takes two steps. First repalce zeros with NaN, then can clean it up:
table2 = standardizeMissing(table1, 0); % the 0 is the value to be replaced with NaN
table2 = rmmissing(table2, 2); % the 2 is for deleting rows; use 1 to delete columns

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-7-19
N = T.Properties.VariableNames;
nvar = length(N);
mask = true(1,nvar);
for K = 1 : nvar
if isnumeric(T.(N{K})) && any(T.(N{K}) == 0, 'all')
mask(K) = false;
end
end
newT = T(:,mask);

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by