필터 지우기
필터 지우기

How to generate random number within specific values

조회 수: 5 (최근 30일)
Med Future
Med Future 2022년 2월 21일
편집: Jan 2022년 2월 21일
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
Jan
Jan 2022년 2월 21일
The question is not clear. Which are the specifications for the random matrix? The saving works by the save command.
Med Future
Med Future 2022년 2월 21일
@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))));

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2022년 2월 21일
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
Med Future
Med Future 2022년 2월 21일
@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 2022년 2월 21일
편집: Jan 2022년 2월 21일
I do not see, where a loop could be useful here. Where does the randomness come into play?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Arif Hoq
Arif Hoq 2022년 2월 21일
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)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by