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

1 次查看(过去 30 天)
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 ...

采纳的回答

Andrei Bobrov
Andrei Bobrov 2017-3-30
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 个评论
Andrei Bobrov
Andrei Bobrov 2017-3-31
x = readtable('c.xls')
k = 3;
y = x(:,2:end);
t = sum(y{:,:} ~= 0);
out_table = [x(:,1),y(:,t >= k)];

请先登录,再进行评论。

更多回答(0 个)

类别

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