0を代入するシステム
13 views (last 30 days)
Show older comments
ランダムで生成された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
↑のような感じです。
0 Comments
Answers (1)
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
②条件文をつける
例えば負の値だけ0にしたいという場合は以下のようにします。
B = A;
idx = A < 0;
B(idx) = 0
0 Comments
See Also
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!