0を代入するシステム

14 views (last 30 days)
einen
einen on 26 Aug 2022
Answered: Hernia Baby on 26 Aug 2022
ランダムで生成された1行7列の行列の数字のどこかしらに0を代入することがうまくできません。
次のような行列Aがあったとします。
A=0.35
0.53
-0.47
0.22
-0.26
0.45
-0.34
この行列のどこかしらに0を代入できるようにしたいです。
B=0.35
0
-0.47
0.22
-0.26
0.45
0
↑のような感じです。

Answers (1)

Hernia Baby
Hernia Baby on 26 Aug 2022
まずはAを設定します。
A = [0.35
0.53
-0.47
0.22
-0.26
0.45
-0.34];
ここでは2つ方法を以下に提案いたします。
①行番号の指定
 決めた行目を0にします。
 まとめて書くには大かっこ [ ] を使用します。
B = A;
B([1,end],1) = 0
B = 7×1
0 0.5300 -0.4700 0.2200 -0.2600 0.4500 0
②条件文をつける
 例えば負の値だけ0にしたいという場合は以下のようにします。
B = A;
idx = A < 0;
B(idx) = 0
B = 7×1
0.3500 0.5300 0 0.2200 0 0.4500 0

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!