How to add a small random number to all values in a matrix
조회 수: 29 (최근 30일)
이전 댓글 표시
Hey Guys I'm going to make a plot from a matrix that is M and N long. I'm going to have a lot of points that would have the same value and I need to add a small random number between -0.1 and 0.1 to all values in the matrix, otherwise the points will be overlapping and I wouldn't be able to tell the different dots apart. Thinking about making a for loop and add a random number to all values, what do you say?
and the plot itself, which in-build function is the easiest to use in this case? scatter, plotmatrix or a thirds one? Thank you in advance! -David
댓글 수: 0
채택된 답변
David Fletcher
2018년 5월 5일
testMatrix=[1 2 3;4 5 6;7 8 9]
noise=(rand(size(testMatrix,1),size(testMatrix,2))-0.5)./5
testMatrix=testMatrix-noise
댓글 수: 0
추가 답변 (3개)
Star Strider
2018년 5월 5일
Try this:
A = YourArray
[M,N] = size(A);
A = A + 0.2*rand(M,N)-0.1;
Experiment to get the result you want.
댓글 수: 0
John D'Errico
2018년 5월 5일
편집: John D'Errico
2018년 5월 5일
Others have shown how to use RAND. RANDN is sometimes appropriate. Sometimes RAND. Some of the time you simply don't care, you just want fuzz. Or sometimes you might want proportional fuzz. So I'll show how you might create each variation.
I'll assume a vector or array X0. Any size or shape array will suffice.
So if you want a spread of +/- delta, using rand, I would do it as
X = X0 + (2*rand(size(X0)) - 1)*delta;
So I multiplied rand by 2, so it now lives in [0,2]. Subtract 1, and it lives in [-1,1]. Then multiply by delta, and the fuzz lives in [-delta,delta]. So essentially uniform random noise.
Next, how would I create proportional noise?
X = X0.*(1 + (2*rand(size(X0)) - 1)*delta);
Here, I've added uniform random fuzz to the number 1. Then multiplied it by X0. The proportional fuzz will now have magnitude delta, but in a proportional sense.
Finally, how about randn? Gaussian fuzz is sometimes appropriate. A linear regression implicitly assumes the noise structure is normally distributed. Not the end of the world if it is not, although proportional noise can cause a problem in regression modeling.
Gaussian noise from randn has mean zero, and standard deviation 1. Multiply it by some constant sigma, and the standard deviation will scale with sigma. So if you want 95% of the fuzz to lie within a spread of delta, then you might do something like this:
X = X0 + randn(size(X0))*delta/1.96;
We get the constant 1.96 from this computation:
normcdf([-1.96 1.96])
ans =
0.024998 0.975
diff(normcdf([-1.96 1.96]))
ans =
0.95
If you wanted to see 50% of the fuzz within a spread of delta, then use this instead:
X = X0 + randn(size(X0))*delta/.67449;
The constant came from
fzero(@(k) diff(normcdf([-1 1]*k)) - 0.5,1)
ans =
0.67449
I suppose you could even create proportional fuzz, generated from randn. Not sure why, but someone would want to do so. ;-)
Finally, be careful if your array is integer, thus of class uint8, or such.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!