How to add a small random number to all values in a matrix

34 次查看(过去 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

采纳的回答

David Fletcher
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

更多回答(3 个)

Star Strider
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.

John D'Errico
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.

David Hindahl
David Hindahl 2018-5-7
Hey Guys, thanks for your help, it worked out :-D

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by