How to produce Gaussian random variable between two vectors ?

조회 수: 4 (최근 30일)
charu shree
charu shree 2023년 5월 3일
편집: Walter Roberson 2023년 5월 4일
Hello all, I am having two vectors:
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
ta = [2,3,6,8,6,4,7,6,6]; % various possible targets
I want to generate Gaussian random variable (rv) between corresponding source and target so that we get 9 different Gaussian rv i.e., first rv between 1 and 2, second rv between 2 and 3, third rv between 2 and 6 and so on.
This is what I had tried so far:
clc;
clear all;
close all;
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
ta = [2,3,6,8,6,4,7,6,6]; % various possible targets
for i = 1:length(sr)
for j = 1:length(ta)
nrm_rv1 = random('norm', 0, sqrt(2)); %generate Gaussian (Normal) RV
end
end
But I am not getting how to arrange nrm_rv1 so as to get 9 different Gaussian rv.
Any help in this regard will be highly appreciated.

채택된 답변

Torsten
Torsten 2023년 5월 3일
편집: Torsten 2023년 5월 3일
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
for i = 1:numel(sr)
pd{i} = makedist('Normal',0, sqrt(2)); %generate Gaussian (Normal) RV
end
pd
pd = 1×9 cell array
Columns 1 through 6 {1×1 prob.NormalDistribution} {1×1 prob.NormalDistribution} {1×1 prob.NormalDistribution} {1×1 prob.NormalDistribution} {1×1 prob.NormalDistribution} {1×1 prob.NormalDistribution} Columns 7 through 9 {1×1 prob.NormalDistribution} {1×1 prob.NormalDistribution} {1×1 prob.NormalDistribution}
If you mean something else when you write "I want to generate Gaussian random variable (rv) between corresponding source and target", please explain it in more detail.
  댓글 수: 3
Torsten
Torsten 2023년 5월 4일
편집: Torsten 2023년 5월 4일
I don't know what you want to do with the 9 normal distributions.
Here are some gimmicks:
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
for i = 1:numel(sr)
pd{i} = makedist('Normal',0, sqrt(2)); %generate Gaussian (Normal) RV
end
random(pd{1})
ans = -0.5965
cdf(pd{4},0.5)
ans = 0.6382
pdf(pd{6},4)
ans = 0.0052
Did you study this ?
charu shree
charu shree 2023년 5월 4일
@Torsten Thank u so much sir...

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

추가 답변 (1개)

Luca Ferro
Luca Ferro 2023년 5월 3일
편집: Luca Ferro 2023년 5월 3일
I would do it like this (assuming i understood correctly):
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
ta = [2,3,6,8,6,4,7,6,6]; % various possible targets
for ii = 1:size(sr,2) %assuming that sr and ta have the same dimension
lowThs=min(sr(ii),ta(ii));
highThs=max(sr(ii),ta(ii));
nrm_rv1(ii) = lowThs+abs(lowThs-highThs)*rand(1,1);
end
i'll break it down:
  • lowThs is the lowest number of the 2 in the array (i assumed no masking is done to assure that the sr value is higher than the ta value)
  • highThs is the highest, same reasoning
  • rand(1,1) generates a random number between 0 and 1
  • abs(lowThs-highThs) this gets the span of the range between the 2 numbers low/highThs
  • abs(lowThs-highThs)*rand(1,1); so by doing this we get a random number from 0 to the span
  • lowThs+abs(lowThs-highThs)*rand(1,1); by adding the lowThs we get a random number that spans from lowThs to lowThs+the random number in the spane, meaning from lowThs to highThs
  • nrm_rv1(ii) this is just to store it in an array. First element is random between the first couple, second between the second and so on
Note that this code works even if ta>sr and ta==sr so that you have freedom on the source arrays. The only constraint is that they must have the same dimension. If you would like to have freedom in that as well just ask, i'll gladly fix the code.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by