How to skip zero columns with MatLab?
古いコメントを表示
Hello,
I have a 2048x65 data matrix called data1 which includes some colums with only zero values. I want my code to skip those zero colums.
I already tried with
dat1 = nonzeros(data1);
data_1 = reshape(dat1,2048,65);
but it seems, that the values are resorted in a wrong order.
So I tried this:
line = 823;
column = 4;
line_new = 1;
spalte_new = 1;
data_1 = 1;
for dat1 = data1(line, colums)
if dat1 ~= 0
data_1(line_new, spalte_new) = dat1(line, column)
column = column + 1
column_new = column_new +1
else
column = column +1
end
end
I also tried with the continue statement, but failed as well.
Could someone help me please? ~Johanna
回答 (2 件)
Walter Roberson
2020 年 5 月 7 日
col_has_nonzero = any(data1 ~= 0,1);
data1_no_zerocol = data1(:, col_has_nonzero);
KSSV
2020 年 5 月 7 日
Check the demo example:
A = rand(10) ;
% Replace few columns with zero dor demo
[m,n] = size(A) ;
A(:,3) = 0 ;
A(:,9) = 0 ;
% Remove zero columns
idx = sum(A==0,1) ;
A(:,idx~=0 ) = []
カテゴリ
ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!