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일

0 개 추천

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

armin m
armin m 2021년 11월 30일
편집: armin m 2021년 11월 30일
Gaussian noise means normal distributed nosie also?tnx. Yes i wanna be normal distributed noise.
dpb
dpb 2021년 11월 30일
Well, that doesn' jibe w/ " 1 to 5 percent of it,s actual value to it," very well.
The normal distribution is unbounded with central tendency to the mean. But the number of values near the mean will be more than those farther away and all you can do is set the distribution nominal standard deviation such that P(abs(Noise)==5) is some percentage. If that is 95% (two-sided), then 5% of all the samples will actually have a noise factor >5%.
You can, of course, narrow the range to essentially make the P(>=5) arbitrarily close to zero and in practice probably never generate a value that large, but when you do so, the whole distribution is compressed.
Sometimes people truncate distributions at some point and that is also, possible, but then the resulting distributions are not strictly following the underlying distribution. How far off that might be is, of couse, totally dependent upon the choices one makes for the distribution parameters again.
DGM
DGM 2021년 11월 30일
I was interpreting the requirements as loosely as their vagueness allows. If the goal were to use a gaussian random factor, then maybe it's reasonable to assume the intent was to set sigma to 1-5% of A, since that's an apropos parameter, where the extreme range really doesn't make as much sense. I'm just throwing out guesses here.
armin m
armin m 2021년 11월 30일
So if i wanna set 96 accuracy for q=[5,7,9,8,7] it means max 4 percent can be added. Which function can do that?
dpb
dpb 2021년 12월 1일
" set 96 accuracy for q=[5,7,9,8,7] it means max 4 percent can be added."
I have no idea what you're trying to say here. Give us an example of what you mean and how ti relates to the above.
Only a distribution with a bounded pdf function can guarantee never going over some limit.
armin m
armin m 2021년 12월 1일
편집: armin m 2021년 12월 1일
I just wanna add some percent to a matrix. The only thing that should be mention is that error should satisfying normal distribution
For a moment, let's ignore the array and just look at the noise. Since the numbers are changing, I'm just going to assume that the goal is that the noise value should be within +/- 5%.
One way to try to keep normally-distributed random numbers within a finite interval is to make the distribution significantly narrower than the interval. That does not guarantee that no random values lie outside the specified interval, it merely reduces the probability that they lie outside. In this case, the gaussian is set up such that the tolerance window is 6 sigma wide. Consequently, R has a 99.999999803% probability of being within tolerance.
% assume intended noise is +/- 5%
tol = 0.05;
k = 6; % sigma scaling factor
R = tol/k*randn(1000);
histogram(R)
[min(R(:)) max(R(:))]
ans = 1×2
-0.0387 0.0420
If you want a larger sigma for the same tolerance or are otherwise unable to accept noise outside the specified tolerance, you'll have to generate a truncated gaussian. That would be a bit more involved.
Once you have R that meets your requirements, adding it is obviously the same as before
B = A + R*A;
armin m
armin m 2021년 12월 1일
I use mat2016
DGM
DGM 2021년 12월 1일
Everything that's been described so far should work in R2016a/b
armin m
armin m 2021년 12월 1일
편집: armin m 2021년 12월 1일
It means that every number i add to my matrix should be betweeen min and max of R?
dpb
dpb 2021년 12월 1일
편집: dpb 2021년 12월 1일
Another poster asked about a RNV within bounds yesterday...depending on how you set mu, sigma, the Answer I posted there may work for you here as well...
In the above the desired limits were Z=+/-2 which is 95% so roughly with a small enough sample size that the likelihood of not exceeding the Z were good enough to probably get by. If your sample size goes way up, then the probability of drawing a value in the tails goes up until at some point it'll be highly unlikely to ever draw a sample that is within the bounds unless you do constrict the variance or truncate the distribution.
"There is no free lunch!" You simply can't have a normally-distributed variate and constrain the tails values and yet allow an arbitrary variance.
Alternatively, just use a uniform or if you do want a weighted to the center distribution, a triangular or other arbitrarily shaped distribution function that is bounded. But, they won't be normal -- can't have both, other than by the illustrated route of constraining the variance to make the probability of exceeding a tail value acceptably low such that it just doesn't occur. The downside to the latter is that you end up with effectively a much smaller error than the max almost always--in the above example the range didn't exceed 0.04 in the sample of (1000)^2 (randn(N) generates a NxN array, not a vector of length N) even though he set the tolerance for 6-sigma to be 5%, not 4% and the effective max range looking at the histogram tails is more nearly in the 3% range. Is that the behavior you're looking for?
DGM
DGM 2021년 12월 1일
편집: DGM 2021년 12월 1일
Extending the same example as before, but with a wider scaling factor for sigma and truncating the distribution:
tol = 0.05;
k = 2;
pd = makedist('Normal','mu',0,'sigma',tol/k);
pdt = truncate(pd,-tol,tol);
R = random(pdt,1000);
histogram(R)
[min(R(:)) max(R(:))]
ans = 1×2
-0.0500 0.0500
I neglected to mention in the prior example that in order for R and A to be combined, you'd need to generate R such that it's the same size as A. In these examples, I'm just generating a large set so that the shape of the distribution is obvious.
As dpb mentions, I'm asserting that the tolerance is +/-5% for the sake of making a consistent example. That should be easy enough to change. I just don't want to chase moving targets.
With the prior example using an unconstrained random factor, you may choose to define the tolerance window as the interval in which a specified fraction of the random values will lie. For example, a scaling factor of 2 would be 95%
tol = 0.05;
k = 2;
s = diff(normcdf([-tol tol],0,tol/k))
s = 0.9545
R = tol/k*randn(1000);
histogram(R)
[min(R(:)) max(R(:))]
ans = 1×2
-0.1234 0.1152
3 would be 99.7% and so on...
k = 3;
s = diff(normcdf([-tol tol],0,tol/k))
s = 0.9973
Or you could find some other way to define the relationship between the tolerance window and the gaussian shape, e.g. you could say that the noise a zero-mean gaussian with 10% FWHM.
tol = 0.05;
k = sqrt(2*log(2))
k = 1.1774
s = normpdf([-tol 0],0,tol/k);
s(1)/s(2)
ans = 0.5000
armin m
armin m 2021년 12월 1일
편집: armin m 2021년 12월 1일
Thank you alot.i am reading it. If i have any question i will ask you again
armin m
armin m 2021년 12월 2일
편집: armin m 2021년 12월 2일
It means i should do this to put the result in specific area with 95 percent accuracy. A=A+random[-0.1234 0.1152]×A ??
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일

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.

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

질문:

2021년 11월 30일

댓글:

2021년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by