Borrar filtros
Borrar filtros

How do we count non-zero entries in every column in a table?

1 visualización (últimos 30 días)
Dear experiences
i have a sparse matrix stored in an excel file, i read this matrix using (read table), i need to count every non-zero entries in every column and remove columns that do not satisfy condition (like K where k=3) such that remove columns that involve 1 and 2 non-zero entries and saved others. note: when columns are removed must be removed along with its header or "title" information using Matlab 2015?
thanks for any participation ...

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 30 de Mzo. de 2017
Let T - your table:
x = [0 0 193 0 0 0 37 0;
53 0 0 0 0 0 0 0;
0 161 0 0 0 0 0 0;
0 0 47 160 0 0 6 15;
0 186 98 0 0 0 0 0;
0 147 125 53 0 0 34 0;
0 0 0 0 0 0 196 0;
0 0 0 0 0 0 143 164;
0 0 0 0 0 0 101 0;
102 92 0 145 0 0 0 0];
xc = num2cell(x,1);
T = table(xc{:},'VariableNames',cellstr(strcat(string('column_'),string(1:numel(xc)))));
k = 3;
t = varfun(@(x)sum(x~=0),T ,'OutputFormat','uniform');
% or t = sum(T{:,:} ~= 0);
out_table = T(:,t >= k);
  3 comentarios
Andrei Bobrov
Andrei Bobrov el 31 de Mzo. de 2017
x = readtable('c.xls')
k = 3;
y = x(:,2:end);
t = sum(y{:,:} ~= 0);
out_table = [x(:,1),y(:,t >= k)];
ahmed obaid
ahmed obaid el 31 de Mzo. de 2017
thanks a lot, very great code

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by