non zero elements in an array
1 回表示 (過去 30 日間)
古いコメントを表示
if a=[1 2 4 5 3 0 0 8 0]
how can i make the elements greater than or equal to 2 as 1 and smaller than two as 0.
so that it become like this:
[0 1 1 1 1 0 0 1 0]
0 件のコメント
採用された回答
Arif Hoq
2022 年 2 月 12 日
a=[1 2 4 5 3 0 0 8 0];
[idx]=find(a<2);
a(idx)=0;
out=a;
[idx2]=find(out>=2);
out(idx2)=1
その他の回答 (2 件)
DGM
2022 年 2 月 12 日
編集済み: DGM
2022 年 2 月 12 日
I'm going to assume you're only dealing in integers here, otherwise the question arises whether values between 1 and 2 should really be preserved.
a = [1.5 1 2 4 5 3 0 0 8 0];
b = max(min(a,1),0)
If values between 1 and 2 should be preserved, then:
c = max(a,0);
c(c>=2) = 1
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!