I will appreciate any suggestion on this simple code. I want to substitute the elements of x >5 with 6 and x<5 with zero.

 채택된 답변

idx=x>5
x(idx)=6
x(~idx)=0

댓글 수: 4

Please, could you be more explicit with your answer.
In the event when I have two conditions to satisfy, please assist:
for j=1:1:8 x=[1 2 3 4 5 6 7 8]; y(j)=x(j).^2; idx=(x>pi&&x<7); idy=y<2; y(idy)=0; y(idx)=90; end
Don't define x inside your loop. It's unnecessary and is just going to waste time.
Don't use a loop; vectorize your code as Azzi suggested.
&& is intended to short-circuit when the two criteria you're trying to AND together evaluate to a scalar value. In this case, your criteria do not evaluate to scalar values. In that case, use & to perform elementwise AND.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2015년 8월 15일

답변:

2015년 8월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by