Add random numbess to matrix

조회 수: 7 (최근 30일)
armin m
armin m 2021년 11월 30일
댓글: armin m 2021년 12월 2일
Hi. I have 1×n matrix.i wanna add 1 to 5 percent of it,s actual value to it, randomly. Can any body help.me? Tnx
  댓글 수: 2
dpb
dpb 2021년 11월 30일
" add 1 to 5 percent of it,s actual value to it, randomly."
The above leaves a lot of uncertainty as to what you intend.
For starters what properties should the random variable have -- normally distributed, uniform, ...
Should the values added always be positive additions (which raises the overall mean of the observations) or zero mean or...
So many Q?, so little info...all we know for sure is you need n RVs...which is an easy thing to generate in MATLAB given a way to select the remaining undefined things like which distribution, what parameters for said distribution, ...
armin m
armin m 2021년 11월 30일
Q is matrix, i wanna it be normal distribute.

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

채택된 답변

DGM
DGM 2021년 11월 30일
편집: DGM 2021년 11월 30일
Define "add 1-5% of its actual value to it"
Consider an array A. Does this mean
% random factor is scalar
B = A + (0.01+0.04*rand(1))*A;
or maybe
% random factor is an array
% each element gets its own random factor
B = A + (0.01+0.04*rand(size(A))).*A;
Imnoise() allows the application of additive gaussian noise using intensity mapping of local noise variance. That might also apply.
  댓글 수: 16
DGM
DGM 2021년 12월 2일
I was just showing how to find particular values of k. In that example, 95% of the noise lies within +/-tol when k = 2. If that's the desired scenario, then:
A = 1:10; % <-- a 1xn vector
tol = 0.05; % set to 4% or whatever you need
k = 2; % sigma scaling factor
R = tol/k*randn(size(A));
B = A + A.*R
B = 1×10
1.0212 1.8844 2.9885 3.9351 4.9835 5.9440 6.6925 7.9764 8.9043 10.0414
armin m
armin m 2021년 12월 2일

Thank you very much

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

추가 답변 (1개)

dpb
dpb 2021년 12월 1일
If the desire is a bounded, symmetric, continuous distribution that approximates a normal, consider the beta with, eta,gamma equal. The pdf is then bounded between [0, 1] with mean gamm/(eta+gamma) --> gamma/(2*gamma) --> 0.5 for eta==gamma.
As for the normal, you can shift and scale the generated RNVs generated from random by whatever is needed to match the target range.
The 'pdf' normalization inside histogram results in the red overlaid normal; scaling the N() pdf to match the peak bin in the histogram results in the black overlay which emphasizes the extra weight of the beta towards the central tendency as compared to a normal. But, you can produce a bounded random variate this way that with the very nebulous requirements for the underlying error distribution could surely serve the purpose.
The above was generated by
rB55=random('beta',5,5,1e6,1);
histogram(rB55)
hold on
[mn,sd]=normfit(rB55)
pN55=normpdf(x,0.5,sd);
plot(x,pN55,'-r')
plot(x,pN55*2.48/2.64,'-k')
hLg=legend('pdf(\beta(5,5))','N(0.5,sd(\beta)','0.94*N(0.5,sd(\beta)');
where the magic constants were obtained by getting the maxima of the histogram binned values and the pdf peak
The above uses functions in the Statistics Toolbox...
  댓글 수: 1
dpb
dpb 2021년 12월 1일
Illustrates can have very broad to quite narrow range depending on the input paramters. While not plotted, note that the B(1,1) case reduces to the uniform distribution.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by