generate a table of random number in specific range

14 次查看(过去 30 天)
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