Borrar filtros
Borrar filtros

How to generate random number within specific values

5 visualizaciones (últimos 30 días)
Med Future
Med Future el 21 de Feb. de 2022
Editada: Jan el 21 de Feb. de 2022
Hi all, i hope you are doing well.
i want to generate random number of specific range using for loop and saved the array in mat file
how can i do that
i have the following code which pick value manually
like prfSTG value between 200 to 1200
and n_pulsesSTG value between 200 to 1000
prfSTG = [200 400 800 1000 1200 ];
n_pulsesSTG = [800 800 800 800 800 800 ];
prfSTG = repmat(prfSTG,1,(ceil(sum(n_pulsesSTG)/length(n_pulsesSTG))));
  2 comentarios
Jan
Jan el 21 de Feb. de 2022
The question is not clear. Which are the specifications for the random matrix? The saving works by the save command.
Med Future
Med Future el 21 de Feb. de 2022
@Jan i want to make random array like
prfSTG value between 200 to 1200
and n_pulsesSTG value between 200 to 1000
and apply for loop in that so the values store in
rfSTG = repmat(prfSTG,1,(ceil(sum(n_pulsesSTG)/length(n_pulsesSTG))));

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 21 de Feb. de 2022
Sorry, your information are still not clear. I try it with a most likely not satisfying solution to demonstrate the problem:
% prfSTG value between 200 to 1200
prfSTG = rand * 1000 + 200;
% n_pulsesSTG value between 200 to 1000
n_pulsesSTG = rand * 800 + 200;
% apply for loop in that so the values store in
for k = 1:1 % completely useless loop
rfSTG = repmat(prfSTG,1,(ceil(sum(n_pulsesSTG)/length(n_pulsesSTG))));
end
What is missing to solve your needs?
  2 comentarios
Med Future
Med Future el 21 de Feb. de 2022
@Jan The following code generate the output like below
prfSTG = [200 400 800 1000 1200 ];
n_pulsesSTG = [800 800 800 800 800 800 ];
prfSTG = repmat(prfSTG,1,(ceil(sum(n_pulsesSTG)/length(n_pulsesSTG))));
But i manully gives the values in array in prfSTG and n_pulsesSTG , i want to generate it automatically ,using loop
Jan
Jan el 21 de Feb. de 2022
Editada: Jan el 21 de Feb. de 2022
I do not see, where a loop could be useful here. Where does the randomness come into play?

Iniciar sesión para comentar.

Más respuestas (1)

Arif Hoq
Arif Hoq el 21 de Feb. de 2022
Try this:
prfSTG=randi([200 1200], 1,5); % generating random number between 200 and 1200
n_pulsesSTG = randi([200 1000], 1,5); % generating random number between 200 and 1000
C=cell(1,5)
C = 1×5 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
for i=1:length(prfSTG)
C{i} = repmat(prfSTG(i),1,(ceil(sum(n_pulsesSTG)/length(n_pulsesSTG(i)))));
end
Matrix=[C{:}];
output=reshape(Matrix,length(C{1, 1}),[]);
plot(output, 'LineWidth', 3)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by