フィルターのクリア

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

1 回表示 (過去 30 日間)
ahmed obaid
ahmed obaid 2017 年 3 月 29 日
コメント済み: ahmed obaid 2017 年 3 月 31 日
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 ...
  2 件のコメント
KSSV
KSSV 2017 年 3 月 30 日
Attach the file...
ahmed obaid
ahmed obaid 2017 年 3 月 30 日
this is my sample table, thanks

サインインしてコメントする。

採用された回答

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)];
ahmed obaid
ahmed obaid 2017 年 3 月 31 日
thanks a lot, very great code

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by