How to skip zero columns with MatLab?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Walter Roberson
el 7 de Mayo de 2020
col_has_nonzero = any(data1 ~= 0,1);
data1_no_zerocol = data1(:, col_has_nonzero);
0 comentarios
KSSV
el 7 de Mayo de 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 comentarios
Ver también
Categorías
Más información sobre Modeling en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!