how can I fill a matrix with randomly -0.01 and 0.01

1 次查看(过去 30 天)
I want to fill a matrix with only two diffrent numbers e.g. 0.01 and -0.01.

采纳的回答

Stephen23
Stephen23 2017-3-23
编辑:Stephen23 2017-3-23
>> V = [0.01,-0.01];
>> V(randi(numel(V),5,5))
ans =
-0.010000 0.010000 -0.010000 0.010000 0.010000
-0.010000 0.010000 0.010000 0.010000 -0.010000
-0.010000 -0.010000 0.010000 0.010000 -0.010000
0.010000 -0.010000 -0.010000 -0.010000 -0.010000
-0.010000 0.010000 -0.010000 0.010000 -0.010000
Although you could calculate the values from randi, this method using indexing has the advantage that the values are not calculated but are exactly those values that you define in V.
  1 个评论
Simon Blum
Simon Blum 2017-3-23
Thanks Stephen. I tried it the following way, but obviously your solution is much better:
V = (2* randi(2,[5,5]) - 3)/100;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by