Matlab/Octave |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Random Number
In this page, I would post a quick reference for Matlab and Octave. (Octave is a GNU program which is designed to provide a free tool that work like Matlab. I don't think it has 100% compatability between Octave and Matlab, but I noticed that most of basic commands are compatible. I would try to list those commands that can work both with Matlab and Octave). All the sample code listed here, I tried with Octave, not with Matlab.
There are huge number of functions which has not been explained here, but I would try to list those functions which are most commonly used in most of matlab sample script you can get. My purpose is to provide you the set of basic commands with examples so that you can at least read the most of sample script you can get from here and there (e.g, internet) without overwhelming you. If you get familiar with these minimal set of functionality, you would get some 'feeling' about the tool and then you would make sense out of the official document from Mathworks or GNU Octave which explains all the functions but not so many examples.
I haven't completed 'what I think is the minimum set' yet and I hope I can complete within a couple of weeks. Stay tuned !!!
< Single Random Number between 0 and 1 >
Case 1 : r = rand(1,1)
Ex)
< Single Random Number between a and b >
Case 1 : r = a + (b-a)*rand(1,1)
Ex)
< N Random Number between 0 and 1 >
Case 1 : r = rand(1,N)
Ex)
< N Random Number between a and b >
Case 1 : r = a + (b-a)*rand(1,N)
Ex)
< Single Random Integer between 0 and N >
Case 1 : r = randi(N)
Ex)
< Single Random Integer between a and b >
Case 1 : r = randi([a b]) // where a and b are all integer
Ex)
< N Random Number between 0 and M >
Case 1 : ri = randi(M,1,N)
Ex)
< N Random Number between a and b >
Case 1 : ri = randi([a b],1,N)
Ex)
< N Random Number of Normal Distribution with mean =0, standard deviation = 1 >
Case 1 : r = randn(1,N)
Ex)
< N Random Number of Normal Distribution with mean =m, standard deviation = 1 >
Case 1 : r = randn(1,N) + m
Ex)
< N Random Number of Normal Distribution with mean =m, standard deviation = s >
Case 1 : r = sqrt(s)*randn(1,N) + m
Ex)
< N Binary Random Number with 0 and 1 >
Case 1 : r = randi([0 1],1,N)
Ex)
< N Binary Random Number with -1 and 1 >
Case 1 : r = 2*randi([0 1],1,N)-1
Ex)
< Complex Random Number with Re = {-1,1}, Im = {-1,1} >
Case 1 : r = (2*randi([0 1],1,N)-1)+j*(2*randi([0 1],1,N)-1)
Ex)
< Generating the same random sequence - Setting the seed number >
Case 1 : rng(SeedNumber)
Ex) In this example, r1 and r3 is generated with the same seed. So r1 and r3 should be same whereas r2 is different.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||