generate a table of random number in specific range

12 views (last 30 days)
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

Answers (2)

Cris LaPierre
Cris LaPierre on 11 Feb 2021
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 on 11 Feb 2021
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

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!