フィルターのクリア

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.

1 回表示 (過去 30 日間)
for j=1:1:8
x=[1 2 3 4 5 6 7 8];
if x(j)>5
x(j)=subs(x(j),x(j),6);
else
x(j)=subs(x(j),x(j),0);
end
end

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 8 月 15 日
idx=x>5
x(idx)=6
x(~idx)=0
  4 件のコメント
Isaac
Isaac 2015 年 8 月 15 日
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
Steven Lord
Steven Lord 2015 年 8 月 16 日
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 件)

matico
matico 2015 年 8 月 16 日
Also:
x=[1 2 3 4 5 6 7 8];
x(x>5) = 6;
x(x<=5) = 0;

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by