Table, delete columns with zero

5 visualizaciones (últimos 30 días)
newbie9
newbie9 el 27 de Ag. de 2019
Respondida: Walter Roberson el 19 de Jul. de 2021
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.)

Respuesta aceptada

newbie9
newbie9 el 27 de Ag. de 2019
Editada: newbie9 el 27 de Ag. de 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 comentario
seema rehman
seema rehman el 19 de Jul. de 2021
It delete all columns if there is any zero

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 19 de Jul. de 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);

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by