How to produce Gaussian random variable between two vectors ?

3 vues (au cours des 30 derniers jours)
charu shree
charu shree le 3 Mai 2023
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.

Réponse acceptée

Torsten
Torsten le 3 Mai 2023
Modifié(e) : Torsten le 3 Mai 2023
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 commentaires
Torsten
Torsten le 4 Mai 2023
Modifié(e) : Torsten le 4 Mai 2023
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 le 4 Mai 2023
@Torsten Thank u so much sir...

Connectez-vous pour commenter.

Plus de réponses (1)

Luca Ferro
Luca Ferro le 3 Mai 2023
Modifié(e) : Luca Ferro le 3 Mai 2023
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.

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by