4

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

Srijan
  • 12,518
  • 10
  • 73
  • 115

1 Answers1

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.

Daryl
  • 5,598
  • 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? Thanks – Srijan Sep 22 '12 at 05:00
  • @srijan: Same matrix, or same matrices? – Rod Carvalho Sep 22 '12 at 05:02
  • @srijan See my edit for specific code to generate the same matrix multiple times. – Daryl Sep 22 '12 at 05:06
  • @RodCarvalho I want same matrix to come every time I run the program.... – Srijan Sep 22 '12 at 05:06
  • @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. – Rod Carvalho Sep 22 '12 at 05:09
  • @Daryl Dear I am getting: undefined function or method 'rng' for input arguments of type 'char'... – Srijan Sep 22 '12 at 05:10
  • 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. – Daryl Sep 22 '12 at 05:12
  • @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... – Srijan Sep 22 '12 at 05:13
  • 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 and load. – Daryl Sep 22 '12 at 05:17
  • @Daryl thank you very much dear. – Srijan Sep 25 '12 at 09:53