MATLAB: Random matrix with maximum values.

matrixrandom

I am struggling in creating random matrix with not more than 30 values. Like if i go randi (n,m) and n =6, m shouldnt be more than 5.
Is it possible to create random matrix which can be (15,2) cause it also consists not more than 30 values in it.
Not asking for code, asking for some suggestions on what should i look for.

Best Answer

You could do this:
w = floor(sqrt(30))
rows = 1 : w
counter = 1;
for k = 1 : length(rows)
columns(k) = floor(30/rows(k));
for col = 1 : columns(k)
fprintf('For %d rows, and %d columns, there are %d elements.\n',...
rows(k), col, rows(k) * col);
allMatrices{counter} = rand(rows(k), col);
counter = counter + 1;
end
end
You'll get every possible matrix size with 30 or fewer elements in it. And each one will be a cell in the allMatrices cell array. It has to be a cell array rather than a 3-D matrix because the matrices are not all the same size. You'll see in the command window:
w =
5
rows =
1 2 3 4 5
For 1 rows, and 1 columns, there are 1 elements.
For 1 rows, and 2 columns, there are 2 elements.
For 1 rows, and 3 columns, there are 3 elements.
For 1 rows, and 4 columns, there are 4 elements.
For 1 rows, and 5 columns, there are 5 elements.
For 1 rows, and 6 columns, there are 6 elements.
For 1 rows, and 7 columns, there are 7 elements.
For 1 rows, and 8 columns, there are 8 elements.
For 1 rows, and 9 columns, there are 9 elements.
For 1 rows, and 10 columns, there are 10 elements.
For 1 rows, and 11 columns, there are 11 elements.
For 1 rows, and 12 columns, there are 12 elements.
For 1 rows, and 13 columns, there are 13 elements.
For 1 rows, and 14 columns, there are 14 elements.
For 1 rows, and 15 columns, there are 15 elements.
For 1 rows, and 16 columns, there are 16 elements.
For 1 rows, and 17 columns, there are 17 elements.
For 1 rows, and 18 columns, there are 18 elements.
For 1 rows, and 19 columns, there are 19 elements.
For 1 rows, and 20 columns, there are 20 elements.
For 1 rows, and 21 columns, there are 21 elements.
For 1 rows, and 22 columns, there are 22 elements.
For 1 rows, and 23 columns, there are 23 elements.
For 1 rows, and 24 columns, there are 24 elements.
For 1 rows, and 25 columns, there are 25 elements.
For 1 rows, and 26 columns, there are 26 elements.
For 1 rows, and 27 columns, there are 27 elements.
For 1 rows, and 28 columns, there are 28 elements.
For 1 rows, and 29 columns, there are 29 elements.
For 1 rows, and 30 columns, there are 30 elements.
For 2 rows, and 1 columns, there are 2 elements.
For 2 rows, and 2 columns, there are 4 elements.
For 2 rows, and 3 columns, there are 6 elements.
For 2 rows, and 4 columns, there are 8 elements.
For 2 rows, and 5 columns, there are 10 elements.
For 2 rows, and 6 columns, there are 12 elements.
For 2 rows, and 7 columns, there are 14 elements.
For 2 rows, and 8 columns, there are 16 elements.
For 2 rows, and 9 columns, there are 18 elements.
For 2 rows, and 10 columns, there are 20 elements.
For 2 rows, and 11 columns, there are 22 elements.
For 2 rows, and 12 columns, there are 24 elements.
For 2 rows, and 13 columns, there are 26 elements.
For 2 rows, and 14 columns, there are 28 elements.
For 2 rows, and 15 columns, there are 30 elements.
For 3 rows, and 1 columns, there are 3 elements.
For 3 rows, and 2 columns, there are 6 elements.
For 3 rows, and 3 columns, there are 9 elements.
For 3 rows, and 4 columns, there are 12 elements.
For 3 rows, and 5 columns, there are 15 elements.
For 3 rows, and 6 columns, there are 18 elements.
For 3 rows, and 7 columns, there are 21 elements.
For 3 rows, and 8 columns, there are 24 elements.
For 3 rows, and 9 columns, there are 27 elements.
For 3 rows, and 10 columns, there are 30 elements.
For 4 rows, and 1 columns, there are 4 elements.
For 4 rows, and 2 columns, there are 8 elements.
For 4 rows, and 3 columns, there are 12 elements.
For 4 rows, and 4 columns, there are 16 elements.
For 4 rows, and 5 columns, there are 20 elements.
For 4 rows, and 6 columns, there are 24 elements.
For 4 rows, and 7 columns, there are 28 elements.
For 5 rows, and 1 columns, there are 5 elements.
For 5 rows, and 2 columns, there are 10 elements.
For 5 rows, and 3 columns, there are 15 elements.
For 5 rows, and 4 columns, there are 20 elements.
For 5 rows, and 5 columns, there are 25 elements.
For 5 rows, and 6 columns, there are 30 elements.