Removing columns containing zeros

24 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by