4
$\begingroup$

I want to compare efficiency of two iteration methods for computing inverse of a matrices. I want to test performance of these methods on some randomly generated matrices. I want to know can we make such matlab code so that every time we run the program it will return the same randomly generated matrices. Could anybody answer me? I would be very much greatfull to you.

Thanks

1 Answers 1

4

In the new versions, use rng('default') to set the seed for the random number generator.

In older versions, it is something like rand('seed','twister'); but I don't remember exactly.

Either way, the first method is recommended by MATLAB.

Edit: here is code to generate random matrix of size $100$

n=100; %size of matrix rng('default'); % set random seed to matlab default A = rand(n,n); % generate random matrix 

If you run this same code multiple times, you will get the same A each time.

  • 0
    Dear i want to generate 100*100 random matrices. I want the same matrix to come every time i run the program. How to do? Thanks2012-09-22
  • 0
    @srijan: Same matrix, or same matrices?2012-09-22
  • 0
    @srijan See my edit for specific code to generate the same matrix multiple times.2012-09-22
  • 0
    @RodCarvalho I want same matrix to come every time I run the program....2012-09-22
  • 0
    @srijan: Then create a random matrix, store it, and every time you need the same "random" matrix use the stored one. What you want, apparently, is not a random matrix, but an *arbitrary* matrix for testing purposes.2012-09-22
  • 0
    @Daryl Dear I am getting: undefined function or method 'rng' for input arguments of type 'char'...2012-09-22
  • 0
    In that case, your version of matlab is older than mine. Replace that line with the alternative I give in the description above for older versions of matlab.2012-09-22
  • 0
    @RodCarvalho How can i store matrices of higher order? I want to test two methods on the same random matrices say of order 1000*1000...I don't know how can i store them so that i may use them...2012-09-22
  • 2
    You can save it to a .mat file with the commane save [filename] [variables] where I have used square brackets to indicate where you should type your respective parameters. To load them again, use the load command. See [save](http://www.mathworks.com.au/help/matlab/ref/save.html) and [load](http://www.mathworks.com.au/help/matlab/ref/load.html).2012-09-22
  • 0
    @Daryl thank you very much dear.2012-09-25