Optimizing a 'for' loop

3 vues (au cours des 30 derniers jours)
bobby
bobby le 11 Avr 2012
%%Average roughness
for i=1:numel(M)
k= abs((mean(M)-M(i)))/numel(M);
avg=avg+k;
end
With the use of above code, i'm calculating average roughness of a surface. The surface height values are stored in the matrix M. The problem is, it takes half an hour to compute as the matrix is 1024*1280. Any better method to do it? As i have atleast 500 sets of data to compute.

Réponse acceptée

Geoff
Geoff le 11 Avr 2012
It really takes half an hour??? What hardware are you using?
Anyway, the biggest improvement on this code is to stop calculating the mean of M every time round the loop. Do it once before you loop. That would take your complexity down from O(N*N) to O(N).
Mav = mean(M);
Oh, and of course if M is a matrix then mean(M) is going to be a 1280-length vector. I suppose that takes your current complexity up to just about O(N^3). Do you even get the right answer? Or are you expecting that? I'd have thought you do this:
Mav = mean(M(:));
Next, you could stop dividing by numel(M) every iteration and do it once after the loop:
avg = avg / numel(M);
But then, why not dispense with the loop altogether and let MatLab deal with it?
avg = mean(abs(mean(M(:)) - M(:)));
I bet that'll do your 500 data sets in a couple of seconds flat.
  2 commentaires
bobby
bobby le 11 Avr 2012
Holy damn replacing the mean(M) with Mav gave the result in a second flat. Yes, it took 33 minutes on average for 5 matrices. I am running Core 2 Duo @ 2.66, 4 GB RAM. Yes with the loop i am getting the right answer.
Thank you for the help. I will prevent this novice mistake further on.
bobby
bobby le 11 Avr 2012
>>feature memstats
Physical Memory (RAM):
In Use: 2908 MB (b5c0a000)
Free: 352 MB (1606c000)
Total: 3260 MB (cbc76000)
Page File (Swap space):
In Use: 3933 MB (f5da3000)
Free: 1211 MB (4bb71000)
Total: 5145 MB (141914000)
Virtual Memory (Address Space):
In Use: 600 MB (258ac000)
Free: 1447 MB (5a734000)
Total: 2047 MB (7ffe0000)
Largest Contiguous Free Blocks:
1. [at 24430000] 405 MB (19500000)
ans = 424673280
Looks like Chrome is using much valuable RAM.

Connectez-vous pour commenter.

Plus de réponses (1)

Matt Kindig
Matt Kindig le 11 Avr 2012
You algorithm isn't quite clear. What is the dimension of mean(M)? Why do you subtract M(i) from it each time? Can you clarify the formula for average roughness that you are trying to implement?
  1 commentaire
bobby
bobby le 11 Avr 2012
I am using this formula: http://www.imagemet.com/WebHelp/roughness_parameters.htm#Roughness_Average with M=1. The mean is actually mean(M(:)). The absolute value of a height from the mean line is needed for the z value, that is why the need for subtracting it. Thank you for your inquisitiveness, but Geoff answered it for me. I am thankful to you both.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by