How to skip zero columns with MatLab?
13 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Walter Roberson
on 7 May 2020
col_has_nonzero = any(data1 ~= 0,1);
data1_no_zerocol = data1(:, col_has_nonzero);
0 Comments
KSSV
on 7 May 2020
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 ) = []
0 Comments
See Also
Categories
Find more on Modeling in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!