필터 지우기
필터 지우기

How to generate a matrix of random decimal values from -1 to 1?

조회 수: 4 (최근 30일)
Rachel Dawn
Rachel Dawn 2021년 7월 27일
댓글: Rik 2021년 7월 27일
I want to generate a 8x10 matrix full of random decimal values from -1 to 1, each with EXACTLY 6 decimal places (i.e 0.523483, -0.932401, etc.).
I know that this code generates values with 7 decimal places (not sure how to get it to 6). But, I don't know how to automatically generate a matrix full of values like these (without using a for loop). Any ideas? thanks!
format long
disp(single(rand(3)))
  댓글 수: 1
Rik
Rik 2021년 7월 27일
Note that single doesn't have normal decimal places, as it is a floating point number. They take this shape:
(-1)^sign*2^(exponent-127)x1.fraction
%double has -1023 instead of -127
What you want is to round, which might affect the distribution of your values in subbtle and unintuitive ways.

댓글을 달려면 로그인하십시오.

답변 (1개)

KSSV
KSSV 2021년 7월 27일
편집: KSSV 2021년 7월 27일
format long
A = single(rand(3)) ;
fmt = [repmat(' %0.6f',1,3),'\n'];
fprintf(fmt,A)
0.753020 0.794493 0.884238 0.019086 0.490358 0.960622 0.517914 0.845987 0.772295

카테고리

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