Clear Filters
Clear Filters

How to generate random number within specific values

5 views (last 30 days)
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 Comments
Jan
Jan on 21 Feb 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 on 21 Feb 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))));

Sign in to comment.

Accepted Answer

Jan
Jan on 21 Feb 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 Comments
Med Future
Med Future on 21 Feb 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 on 21 Feb 2022
Edited: Jan on 21 Feb 2022
I do not see, where a loop could be useful here. Where does the randomness come into play?

Sign in to comment.

More Answers (1)

Arif Hoq
Arif Hoq on 21 Feb 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)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!