0

This is the code I have so far to compute how long it takes, on average, for MATLAB to implement gaussian elimination on a matrix of size N=200:

Ntr=50; % Number of trials
N=200; % Matrix size

times=zeros(Ntr,1); % Vector of timing data for Ntr trials

for i=1:Ntr

% Form a random matrix A and right-hand side b (normally distributed)
A=randn(N,N);
b=randn(N,1);

% Apply backslash and calculate time taken
tic;
x=A\b;
times(i)=toc;

end

N
mean_time=mean(times)

How can I modify this code so that it computes this for various values of N such as N=200, 500, 1000, 3000 etc? I tried a for loop but randn can only take in scalar values... The end result I am looking for is plotting a loglog graph of N values against the average time taken. Any help would be appreciated!

  • 4
    [`timeit`](http://www.mathworks.com/help/matlab/ref/timeit.html) might be the preferred way for timing those. Also, you can look [`here`](http://stackoverflow.com/a/29719681/3293881) for some benchmarking examples with it. – Divakar Jun 04 '15 at 08:33
  • What do you mean? Do you want `Ntr` to be variable? – kkuilla Jun 04 '15 at 08:33
  • 2
    why not create a second for loop for the N's? – rst Jun 04 '15 at 09:26

1 Answers1

0

You should set a vector with all the size you want to evaluate and in your for loop you then define N as N=All_the_N(i). That way randn only take a scalar value and you iterate over different N.

All_the_N=[100 200 300 400]; %Matrix Size
Ntr=4;
times=zeros(Ntr,1);

for i=1:Ntr

N=All_the_N(1,i);
A=randn(N,N);
b=randn(N,1);

f=@() A/b;
times(i,1)=timeit(f)

end
Dom C.
  • 35
  • 9