Turning a while loop into 1 line

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 件のコメント

Bradley Farman
Bradley Farman 2017 年 6 月 4 日
Never mind, I seem to have figured it out.
newv_sol=v(find(v<30))
Stephen23
Stephen23 2017 年 6 月 4 日
編集済み: Stephen23 2017 年 6 月 4 日
find is not required: logical indexing is more efficient:
newv_sol=v(v<30)

サインインしてコメントする。

回答 (1 件)

ES
ES 2017 年 6 月 5 日

0 投票

newv_sol=v(v<30)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2017 年 6 月 4 日

回答済み:

ES
2017 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by