delete zeros from matrix
2 次查看(过去 30 天)
显示 更早的评论
How do I delete all the columns that have zeros with a for loop? I tried reducing the number of columns by one in every iteration but matlab shows me "index exceeds matrix dimentions".
p4 = [0 0 31 37 43 47; 0 0 19 13 7 3];
grammes = size(p3,1);
sthles = size(p3,2);
for i = 1 : grammes
for j = 1 : sthles
if iszero(p3(i,j)) == 1
p3(:,j) = [];
sthles = sthles - 1
end
end
end
采纳的回答
Bruno Luong
2020-7-23
Here is how using FOR-LOOP to delete zero column(s)
A = [0 0 31 37 43 47;
0 0 19 13 7 3];
[m,n] = size(A);
for j = n:-1:1 % must loop reversing
deleteflag = true;
for i = 1 : m
if A(i,j) ~= 0
deleteflag = false;
end
end
if deleteflag % only true if the entire column #j contains 0s
A(:,j) = [];
end
end
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!