필터 지우기
필터 지우기

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 ...

채택된 답변

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 CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by