http://www.mathworks.com/matlabcentral/newsreader/view_thread/307714
MATLAB Central Newsreader  How to speed up this ForLoop by a vector?
Feed for thread: How to speed up this ForLoop by a vector?
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 13 May 2011 14:20:20 +0000
How to speed up this ForLoop by a vector?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307714#835831
Alex
Hi all,<br>
<br>
i want to speed up a code, where i calculate a local standard deviation within another matrix.<br>
<br>
e.g.:<br>
<br>
mat1 = magic(100);<br>
mat2 = zeros(100,100);<br>
<br>
for i = 8:93<br>
for j = 8:93<br>
mat2(i,j) = std2(i7:i+7,j7:j+7)<br>
<br>
end<br>
end<br>
<br>
the code does exactly what i want, but is very slow. thats why im looking for a fast replacement.<br>
<br>
i know how to adress the indices in a fast way an give them a fixed value<br>
(<br>
mat2(8:(size17),8:(size17)) = 5<br>
) <br>
<br>
but i dont know how to make a fast, dynamic, calculation depending on the position.<br>
<br>
hope you may help me<br>
<br>
thanks a lot<br>
<br>
alex

Fri, 13 May 2011 15:14:05 +0000
Re: How to speed up this ForLoop by a vector?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307714#835843
alistair templeton
<br>
> <br>
> i want to speed up a code, where i calculate a local standard deviation within another matrix.<br>
<br>
i don't know if it's any faster since i don't have std2, but you can express your standard deviation as a convolution:<br>
<br>
mat1 = magic(100);<br>
kernel = ones(15);<br>
<br>
mat2 = sqrt(conv2(mat1.^2,kernel,'same'));<br>
<br>
<br>
<br>
(double check this, it could be totally wrong)

Fri, 13 May 2011 15:33:05 +0000
Re: How to speed up this ForLoop by a vector?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307714#835848
someone
"Alex" wrote in message <iqjen4$pr$1@newscl01ah.mathworks.com>...<br>
> Hi all,<br>
> <br>
> i want to speed up a code, where i calculate a local standard deviation within another matrix.<br>
> <br>
> e.g.:<br>
> <br>
> mat1 = magic(100);<br>
> mat2 = zeros(100,100);<br>
> <br>
> for i = 8:93<br>
> for j = 8:93<br>
> mat2(i,j) = std2(i7:i+7,j7:j+7)<br>
> <br>
> end<br>
> end<br>
> <br>
> the code does exactly what i want, but is very slow. thats why im looking for a fast replacement.<br>
> <br>
> i know how to adress the indices in a fast way an give them a fixed value<br>
> (<br>
> mat2(8:(size17),8:(size17)) = 5<br>
> ) <br>
> <br>
> but i dont know how to make a fast, dynamic, calculation depending on the position.<br>
> <br>
> hope you may help me<br>
> <br>
> thanks a lot<br>
> <br>
> alex<br>
<br>
I don't have the Image Processing Toolbox, <br>
but the documentation says:<br>
<br>
std2(A) computes the standard deviation of the array A using std(A(:))<br>
<br>
If properly used (i.e., preallocation), for loops <br>
are not NECESSARILY slower than vectorized code.<br>
(It depends on many factors such as size of arrays, available memory, etc.)<br>
Aside frrom the obvious typo in the above code:<br>
mat2(i,j) = std2(i7:i+7,j7:j+7)<br>
should be (I assume something like):<br>
mat2(i,j) = std2(mat1(i7:i+7),mat1(j7:j+7)); % note the semicolon at the end<br>
You could try moving some calculations from <br>
from the inner loop to the outer loop <br>
(something like):<br>
<br>
for i = 8:93<br>
mat3 = mat1((i7:i+7);<br>
for j = 8:93<br>
mat4 = mat1(j7:j+7);<br>
mat2(i,j) = std([mat3(:);mat4(:)]); % note std instead of std2<br>
end<br>
end<br>
<br>
Just a thought

Sat, 14 May 2011 03:33:04 +0000
Re: How to speed up this ForLoop by a vector?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307714#835957
Roger Stafford
"Alex" wrote in message <iqjen4$pr$1@newscl01ah.mathworks.com>...<br>
> Hi all,<br>
> <br>
> i want to speed up a code, where i calculate a local standard deviation within another matrix.<br>
> <br>
> e.g.:<br>
> <br>
> mat1 = magic(100);<br>
> mat2 = zeros(100,100);<br>
> <br>
> for i = 8:93<br>
> for j = 8:93<br>
> mat2(i,j) = std2(i7:i+7,j7:j+7)<br>
> end<br>
> end<br>
> .........<br>
> alex<br>
           <br>
What strikes me as inefficient is not your nested forloops, but the fact that large portions of the standard deviation computation over the moving 15 by 15 square are being needlessly repeated in that method. That is, two adjacent 15 x 15 squares have 210 out of a total of 225 elements in common in their overlap area, and it is surely a waste of time to compute the standard deviation of each set of 225 independently of the other. Of course doing so will require a somewhat different, though equivalent, formula for the standard deviation.<br>
<br>
The following computes both the sums of elements (xs) and the sums of their squares (x2s) in the moving 15 x 15 square. By taking appropriate differences of 'cumsum', only a little more than four additions and four subtractions are required per 15 x 15 square in attaining the sums in these squares of their elements and their squares. The line computing 's' below gives the alternate standard deviation formula used with these two quantities.<br>
<br>
% The original data<br>
n = 100; % The size of the original square matrix<br>
m = 15; % Defines the m x m moving square within it<br>
x = randn(n); % Or whatever data you wish<br>
<br>
% The standard deviation computation<br>
t1 = cumsum([zeros(1,n);x],1);<br>
t2 = cumsum([zeros(nm+1,1),t1(m+1:n+1,:)t1(1:nm+1,:)],2);<br>
xs = t2(:,m+1:n+1)t2(:,1:nm+1); % The sums of x over each square<br>
t1 = cumsum([zeros(1,n);x.^2],1);<br>
t2 = cumsum([zeros(nm+1,1),t1(m+1:n+1,:)t1(1:nm+1,:)],2);<br>
x2s = t2(:,m+1:n+1)t2(:,1:nm+1); % The sums of x^2 over each square<br>
s = sqrt((x2sxs.^2/m^2)/(m^21)); % Compute stan. dev. from xs & x2s<br>
<br>
The above only produces the nonzero 86 x 86 interior of mat2. To get your actual result in mat2, add on the following:<br>
<br>
mat2 = zeros(n);<br>
mat2(8:93,8:93) = s;<br>
<br>
Roger Stafford

Sat, 14 May 2011 08:54:20 +0000
Re: How to speed up this ForLoop by a vector?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307714#835976
Alex
Thanks to all.<br>
I will test the code later and reply you then.<br>
<br>
Best regards,<br>
<br>
Alex