What does a(a<0) = 0 mean in matlab?

23 ビュー (過去 30 日間)
Abhinav Agrawal
Abhinav Agrawal 2022 年 6 月 23 日
回答済み: Cris LaPierre 2022 年 6 月 23 日
I am new to matlab.
What does a(a<0) = 0 mean?
is it valid if a is an array as well?

採用された回答

Cris LaPierre
Cris LaPierre 2022 年 6 月 23 日
Yes, it works on arrays. It is using logical indexing to assign values to a. You can learn more about logical indexing in Ch 11 of MATLAB Onramp.
Basically, set any value in a that is less than zero to zero. Here's a simple example.
a = rand(3)
a = 3×3
0.0787 0.4977 0.0631 0.2203 0.4529 0.4654 0.9312 0.7561 0.7632
a<0.5
ans = 3×3 logical array
1 1 1 1 1 1 0 0 0
a(a<0.5)=0
a = 3×3
0 0 0 0 0 0 0.9312 0.7561 0.7632

その他の回答 (1 件)

Adam Danz
Adam Danz 2022 年 6 月 23 日
> What does a(a<0) = 0 mean?
a can be a scalar or an array.
This replaces all values in a that are less than 0 with 0.
Example
a = [-6: 2 : 6]
a = 1×7
-6 -4 -2 0 2 4 6
a(a<0) = 0
a = 1×7
0 0 0 0 2 4 6

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by