Develop a Matlab function that remove all zeros  in an input vector(using while  loop)

1 vue (au cours des 30 derniers jours)
amal -88
amal -88 le 27 Nov 2017
using  while  loop

Réponses (1)

kowshik Thopalli
kowshik Thopalli le 27 Nov 2017
Modifié(e) : kowshik Thopalli le 27 Nov 2017
I agree with cvklpstunc that you dont need a while loop. If you are still looking for a while loop implementation, here is the code
count=1;z=1;
a=[1:10, zeros(5,1)'];
while count<=length(a)
if a(count)==0
zero_indices(z)=count;
z=z+1;
end
count=count+1;
end
a(zero_indices)=[];
Here is one line answer for what you want to do
b=a(find(a))
  2 commentaires
James Tursa
James Tursa le 27 Nov 2017
Please don't provide complete solutions to homework problems. Also your code has problems because it doesn't initialize zero_indices.
kowshik Thopalli
kowshik Thopalli le 30 Nov 2017
James Tursa- Thank you for the suggestion. Will certainly keep in mind from next time.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by