0
a=18;
b=22;
for i=1:10
r1 = randi([18 22],1,1)
name= (b-a).*rand(r1,2) + a
end

now save this all name value matrix in result matrix of all genrated value in each loop row size not fix but coiumn is 2

parker
  • 1
  • 1
  • Possible duplicate of [Matrix of unknown length in MATLAB?](http://stackoverflow.com/questions/1548116/matrix-of-unknown-length-in-matlab) – Wolfie Apr 15 '17 at 07:36

2 Answers2

0

preallocate the maximum sized matrix and remove redundant rows at the end:

a = 18;
b = 22;
% number of iterations
n = 10;
% maximum number of rows
maxRows = b*n;
% preallocating matrix
nameMat = zeros(maxRows,2);
currentRow = 0;
for i = 1:n
    r1 = randi([18 22],1,1);
    name = (b-a).*rand(r1,2) + a;
    nameRows = size(name,1);
    nameMat((1:nameRows) + currentRow,:) = name;
    currentRow = currentRow + nameRows; 
end
% remove redundant rows
nameMat(currentRow+1:end,:) = [];
user2999345
  • 4,195
  • 1
  • 13
  • 20
0

You can achieve it with one line code; actually in each iteration of the loop you:

  • generate an integer random number r1 (e. g. 20)
  • it is then used to generate r1 double random number that are multiplied by (b-a) and then added to a
  • these random numbers are not affected by the ones generated in a previos iteration
  • a and b are constant so do not change in the loop

At the end of your loop you the number of row will be the sum of the integer random number, so you can directly generate the 10 integer random numbers in the evaluation of name and sum them to create the desired set name values:

a=18;
b=22;
n_iter=10;

name=(b-a).*rand(sum(randi([18 22],n_iter,1)),2) + a

Hope this helps,

Qapla'

il_raffa
  • 5,090
  • 129
  • 31
  • 36