generate a table of random number in specific range

조회 수: 22 (최근 30일)
Khairul nur
Khairul nur 2021년 2월 11일
답변: Image Analyst 2021년 2월 11일
Hi, i would like to generate a table with specific number of index range.
For example if k =3, the table [3x3] in specific number of range between 1 ,2 and 3 :
1 2 3
2 1 2
3 3 1

답변 (2개)

Cris LaPierre
Cris LaPierre 2021년 2월 11일
I'd use randi with the following syntax
k=3;
X = randi(k,k)
X = 3×3
1 2 2 3 2 1 3 1 2

Image Analyst
Image Analyst 2021년 2월 11일
Did you look up rand() in the help? You would have learned how to do things like this:
% Define parameters.
minValue = 1;
maxValue = 3;
rows = 3;
% Create matrices:
m = minValue + (maxValue - minValue) * rand(rows)
% Or with integers:
m2 = randi([minValue, maxValue], rows, rows)
% Or with integers and no repeats:
m3 = reshape(randperm(rows^2), rows, [])
You'll see
m =
1.86651555049844 1.13391009681515 1.53934952237305
1.56954330912193 2.22280802997294 2.14026117843922
2.73713895445458 1.68866727412449 1.26436419354187
m2 =
1 1 3
3 1 2
2 3 1
m3 =
5 9 4
6 2 1
3 7 8

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by