Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Vectorization of nested for loops

Subject: Vectorization of nested for loops

From: maxime

Date: 10 Dec, 2010 21:03:05

Message: 1 of 5

Hi,
I use a Matlab routine to compute correlations between two images.
It's very slow when my image are large. So I want to vectorize it (I hope that it will go faster).
My code :
A and B are 2048x2048 images. I analyse the correlation in subwindows 64x64.

for j=1:64:1985
    for i=1:64:1985
        C=A(j:j+63,i:i+63);
        D=B(j:j+63,i:i+63);
        R=real(fftshift(ifft2(fft2(a1(1:64,1:64)).*conj(fft2(a2(1:64,1:64))))));
        cor(i,j)=max(max(R));
    end
end

How can I vectorize it ?
Thank you,
Maxime

Subject: Vectorization of nested for loops

From: Matt J

Date: 10 Dec, 2010 21:41:08

Message: 2 of 5

"maxime " <maxdeff@yahoo.fr> wrote in message <idu4i9$iro$1@fred.mathworks.com>...
> Hi,
> I use a Matlab routine to compute correlations between two images.
> It's very slow when my image are large. So I want to vectorize it (I hope that it will go faster).
> My code :
> A and B are 2048x2048 images. I analyse the correlation in subwindows 64x64.
>

g=@(X) mat2cell(X,ones(1,32)*64,ones(1,32)*64);
f=@(Xc) fft2( cat(3,Xc{:}) );

AA= f(g(A)) ;
BB= f(g(B));

R=real( ifft2( AA.*conj(BB)) );

cor=max(reshape(R,64^2,1024));
cor=reshape(cor,[32,32]);

Subject: Vectorization of nested for loops

From: Sean de

Date: 10 Dec, 2010 21:46:05

Message: 3 of 5

"maxime " <maxdeff@yahoo.fr> wrote in message <idu4i9$iro$1@fred.mathworks.com>...
> Hi,
> I use a Matlab routine to compute correlations between two images.
> It's very slow when my image are large. So I want to vectorize it (I hope that it will go faster).
> My code :
> A and B are 2048x2048 images. I analyse the correlation in subwindows 64x64.
>
> for j=1:64:1985
> for i=1:64:1985
> C=A(j:j+63,i:i+63);
> D=B(j:j+63,i:i+63);
> R=real(fftshift(ifft2(fft2(a1(1:64,1:64)).*conj(fft2(a2(1:64,1:64))))));
> cor(i,j)=max(max(R));
> end
> end
>
> How can I vectorize it ?
> Thank you,
> Maxime


I wouldn't.
I'd preallocate cor:
cor = zeros(length(1:64:1985));
for ii = 1:64:1985
   for jj = 1:64:1985
              R=real(fftshift(ifft2(fft2(A(ii:ii+63,jj:jj+63)).*conj(fft2(B(ii:ii+63,jj:jj+63))))));
       cor(floor(ii/64)+1,floor(jj/64)+1)=max(R(:));
   end
end

Subject: Vectorization of nested for loops

From: Matt J

Date: 10 Dec, 2010 21:58:04

Message: 4 of 5

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idu72t$4e4$1@fred.mathworks.com>...
>
> I wouldn't.
> I'd preallocate cor:
> cor = zeros(length(1:64:1985));
> for ii = 1:64:1985
> for jj = 1:64:1985
> R=real(fftshift(ifft2(fft2(A(ii:ii+63,jj:jj+63)).*conj(fft2(B(ii:ii+63,jj:jj+63))))));
> cor(floor(ii/64)+1,floor(jj/64)+1)=max(R(:));
> end
> end
======

Just as a small point of optimization - there's no reason to be doing fftshift. It doesn't change max(R(:))

R=real((ifft2(fft2(A(ii:ii+63,jj:jj+63)).*conj(fft2(B(ii:ii+63,jj:jj+63))))));

Subject: Vectorization of nested for loops

From: Matt J

Date: 10 Dec, 2010 22:07:21

Message: 5 of 5

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <idu7pc$gdt$1@fred.mathworks.com>...
>>
> Just as a small point of optimization - there's no reason to be doing fftshift. It doesn't change max(R(:))
>
> R=real((ifft2(fft2(A(ii:ii+63,jj:jj+63)).*conj(fft2(B(ii:ii+63,jj:jj+63))))));

You can also shave off a little bit of time by using
ifft2(..,'symmetric') instead of real()

R=ifft2(fft2(A(ii:ii+63,jj:jj+63)).*conj(fft2(B(ii:ii+63,jj:jj+63))),'symmetric');

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us