Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|cite|improve this question
up vote 4 down vote accepted

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.

share|cite|improve this answer
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 5:00
@srijan: Same matrix, or same matrices? – Rod Carvalho Sep 22 '12 at 5:02
@srijan See my edit for specific code to generate the same matrix multiple times. – Daryl Sep 22 '12 at 5:06
@RodCarvalho I want same matrix to come every time I run the program.... – srijan Sep 22 '12 at 5:06
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 5:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.