Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Vectorising nested for loops

1 visualizzazione (ultimi 30 giorni)
Vishnu
Vishnu il 1 Dic 2013
Chiuso: MATLAB Answer Bot il 20 Ago 2021
I need to create slices of 2d matrices(ie a 3D matrix Y in which each slice is a 2D matrix )from a given 2D square matrix (which is the matrix X)..Dimensions of X can be upto 512.If dimensions of X is NxN, then dimensions of Y is NxNx(N/2).
The original code is as follows
N=size(X,1);
M=N/2;
Y(1:N,1:N,1:M)=0;
for k1 = 1:N
for k2 = 1:N
for p= 1:M
for n1=1:N
for n2=1:N
N1=n1-1; N2=n2-1; P=p-1; K1=k1-1; K2=k2-1;
z=mod((N1*K1+N2*K2),N);
if (z==P) -->A
Y(k1,k2,p)= Y(k1,k2,p)+ X(n1,n2);
elsif (z==(P+M))
Y(k1,k2,p)= Y(k1,k2,p)- X(n1,n2);
end
end
end
end
end
Execution time for a 64x64 input is 28.9 seconds. I managed to vectorise the inner two loops and the if statement
The modified code is
N=size(X,1);
M=N/2;
Y(1:N,1:N,1:M)=0;
w=repmat((0:N-1),N,1);
q=w'
for k1 = 1:N
for k2 = 1:N
for p= 1:M
P=p-1;
K1=k1-1;
K2=k2-1;
z = mod((K1*q)+(K2*w),N);
Y(k1,k2,p) =sum(X(z==P))-sum(X(z==P+M));
end
end
end
Now the execution time for a 64x64 matrix input is reduced to 20.2 seconds.
I need to vectorise the remaining 3 loops also..Please help...I need to bring down the execution time to minimum..Thanks again..

Risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by