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
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
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,:) = [];
You can achieve it with one line code; actually in each iteration of the loop you:
integer
random number r1
(e. g. 20
)r1
double
random number that are multiplied by (b-a)
and then added to a
a
and b
are constant so do not change in the loopAt 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'