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

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 Exchange에서 Matrix 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