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

조회 수: 2 (최근 30일)
Simon Blum
Simon Blum 2017년 3월 23일
댓글: Simon Blum 2017년 3월 23일
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