I want to know how can I generate a matrix of random numbers of any given size, for example 2x4
. Matrix should consists of signed whole number in range, for example [-500, +500]
.
I have read the documentation of RNG, but I am not sure on how I should use this. I referred too this question but this did not provide me the solution I am looking for.
I know this might be a silly question, but any help on it would be truly appreciated.
np.random.rand () to create random matrix All the numbers we got from this np.random.rand () are random numbers from 0 to 1 uniformly distributed. You can also say the uniform probability between 0 and 1. Parameters: It has parameter, only positive integers are allowed to define the dimension of the array.
There is a randn () function in the OpenCV libraries, but I don't know how to pass arguments to this function to generate numbers with mean 0 and variance 1. Show activity on this post. OpenCV has a randn () function and also a RNG class. Below is the Matlab code you might want to replace, and the equivalent OpenCV code.
Below is the Matlab code you might want to replace, and the equivalent OpenCV code. Internally, OpenCV implements randn () using RNG. The disadvantage if using randn () is that you lose control over the seed. If matrix2xN above had more than one channel then a different mean/sigma is used for each channel.
Random number generator. It encapsulates the state (currently, a 64-bit integer) and has methods to return scalar random values and to fill arrays with random values. Currently it supports uniform and Gaussian (normal) distributions.
If you want values to be uniformly distributed, you can use cv::randu
Mat1d mat(2, 4); // Or: Mat mat(2, 4, CV_64FC1);
double low = -500.0;
double high = +500.0;
randu(mat, Scalar(low), Scalar(high));
Note that the upper bound is exclusive, so this example represents data in range [-500, +500)
.
If you want values to be normally distributed, you can use cv::randn
Mat1d mat(2, 4); // Or: Mat mat(2, 4, CV_64FC1);
double mean = 0.0;
double stddev = 500.0 / 3.0; // 99.7% of values will be inside [-500, +500] interval
randn(mat, Scalar(mean), Scalar(stddev));
This works for matrices up to 4 channels, e.g.:
Mat3b random_image(100,100);
randu(random_image, Scalar(0,0,0), Scalar(256,256,256));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With