Removing columns containing zeros

조회 수: 17 (최근 30일)
Oliver Vavasour
Oliver Vavasour 2016년 4월 27일
답변: Andy 2018년 5월 20일
I have a 2x500 matrix containing data. Some columns contain only zeros and I need to remove these (forming, say, a 2x460 matrix when 40 columns contained zeros).
Similar questions have been asked and answered about cell arrays which give commands such as:
m = m(cellfun(@ischar,m(:,1)),:);
These commands produce the error 'Input #2 expected to be a cell array, was double instead' when I use my matrix in place of 'm'

채택된 답변

Torsten
Torsten 2016년 4월 27일
m=m(:,any(m))
Best wishes
Torsten.
  댓글 수: 1
Oliver Vavasour
Oliver Vavasour 2016년 4월 27일
Perfect solution, thank you

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andy
Andy 2018년 5월 20일
If you've got a table w/ various variable names that you want to keep, you might do:
function pTable = aRemoveZeroTableColumns(primersTable)
%
%
%
pTable = primersTable;
indexes = [];
for i = 1:numel(primersTable.Properties.VariableNames)
column = primersTable{:,i};
if all(column == 0)
indexes = [indexes, i];
end
end
pTable(:,indexes) = [];
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by