MATLAB provides several functions to generate random numbers. The rand
function generates uniformly distributed numbers between 0 and 1. The randn
function generates normally distributed random numbers.
Here's an example of using rand
to generate a random matrix of size 3x3:
main.m139 chars3 lines
Output:
main.m98 chars6 lines
You can also specify the range of the random numbers using the rand
function. For example, to generate a random number in the range [a,b], use:
main.m19 chars2 lines
where a
and b
are the lower and upper bounds, respectively.
main.m49 chars5 lines
Output:
main.m16 chars4 lines
Similarly, you can use the randn
function to generate normally distributed random numbers with mean 0 and standard deviation 1. The randn
function takes the same input arguments as rand
.
gistlibby LogSnag