Optimizing a 'for' loop

3 次查看(过去 30 天)
bobby
bobby 2012-4-11
%%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.

采纳的回答

Geoff
Geoff 2012-4-11
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 个评论
bobby
bobby 2012-4-11
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 2012-4-11
>>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.

请先登录,再进行评论。

更多回答(1 个)

Matt Kindig
Matt Kindig 2012-4-11
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 个评论
bobby
bobby 2012-4-11
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Least Squares 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by