Turning a while loop into 1 line
Afficher commentaires plus anciens
Hi, for an assignment, I have to turn this while loop into one line
v=[4 11 22 5 33 -8 3 99 52];
newv_sol=[];
j=1;
i=1;
while j<=length(v)
if v(j)<30
newv_sol(i)=v(j);
i=i+1;
end
j=j+1;
end
newv_sol
So far, I've only managed to make it in two lines with the find function and logical indexing.
x=find(v<30)
newv_sol=v(x)
Is there a way to combine these two?
2 commentaires
Bradley Farman
le 4 Juin 2017
find is not required: logical indexing is more efficient:
newv_sol=v(v<30)
Réponses (1)
ES
le 5 Juin 2017
0 votes
newv_sol=v(v<30)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!