1
$\begingroup$

I have made following matlab code for computing cpu time of matlab inbuilt function pinv(A) to compute the Pseudo inverse of a given matrix. I had runed the matlab code 1000 times & calculated the mean of the results.

Matlab code

A = rand(10) % given randomly generated matrix of order 10 REPS = 1000; % taking thousand times repetitions   minTime = Inf;   tic for i=1:REPS     tstart = tic;        x1=pinv(A)      telapsed = toc(tstart);      minTime = min(telapsed,minTime); end  execTime = toc; % total execution time  averageTime = execTime/REPS 

I am not sure whether my program is correct or not? I have to use these results for my project work. Could anybody help me with this. I would be very much thankful to you.

  • 1
    averagetime should be the average time of each iteration. What is your problem exactly?2012-09-18
  • 1
    I edited your code. I changed it slightly. `execTime` is the total execution time. `averageTime` is the average time to calculate the pseudoinverse. `minTime` is the minimum time for one iteration. You are also likely to get more accurate results if each iteration takes longer (See [here](http://scicomp.stackexchange.com/questions/3141/understanding-wall-time-jitter-in-matlab-computations) for issues when the execution time is too short). The easiest way to do this is to increase the size of `A`, such as `A=rand(100)`. Depending on your machine, this still may not be large enough.2012-09-18
  • 0
    @Daryl Thanks a lot for helping me. I wanted to know whether my code is correct or not? Thanks again..:)2012-09-18
  • 1
    The edited code will work as you desire. As I said, you may not experience accurate results for the timing with `A` so small in size. As I suggested above, make the coefficient matrix `A` larger, so that the total execution time is longer for each iteration. (I will post these comments as an answer.)2012-09-18
  • 0
    @Daryl: Don't you want `A = rand(10)` to be inside the loop? This way you'll just get the average time it takes to invert one particular random matrix; if you want to average over different random matrices, you need to put the line that generates the random matrices inside the loop.2012-09-19
  • 1
    @joriki You will then incur additional execution time for the generation of new random matrices, in the outer tic/toc counter. The particular inverse is not important here, just the execution time to calculate one of them.2012-09-19

1 Answers 1

2

(Copied from above comments) I edited your code. I changed it slightly. execTime is the total execution time. averageTime is the average time to calculate the pseudoinverse. minTime is the minimum time for one iteration. You are also likely to get more accurate results if each iteration takes longer (See here for issues when the execution time is too short). The easiest way to do this is to increase the size of A, such as A=rand(100). Depending on your machine, this still may not be large enough.

The edited code will work as you desire. As I said, you may not experience accurate results for the timing with A so small in size. As I suggested above, make the coefficient matrix A larger, so that the total execution time is longer for each iteration. (I will post these comments as an answer.)

  • 0
    Ya took larger order random generated matrix.2012-09-18
  • 1
    Yes. I did that so that in the `pinv(..)` function didn't execute too quickly. I'm not sure about your machine, but with `A=rand(10)`, (my version of) `pinv(..)` was executing too quickly for tic and toc to accurately register elapsed times. Changing `A` to be larger resolved this problem, at the expense of using more memory. This may not be a problem on you machine, I am unsure.2012-09-18
  • 0
    Ya i did for larger order matrices. For smaller order matrices it is taking very less time.2012-09-18