Table, delete columns with zero

13 vues (au cours des 30 derniers jours)
newbie9
newbie9 le 27 Août 2019
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.)

Réponse acceptée

newbie9
newbie9 le 27 Août 2019
Modifié(e) : newbie9 le 27 Août 2019
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 commentaire
seema rehman
seema rehman le 19 Juil 2021
It delete all columns if there is any zero

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 19 Juil 2021
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);

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by