generate a table of random number in specific range

12 ビュー (過去 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 Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by