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

2 Ansichten (letzte 30 Tage)
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 ...

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 30 Mär. 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 Kommentare
Andrei Bobrov
Andrei Bobrov am 31 Mär. 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 am 31 Mär. 2017
thanks a lot, very great code

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by