Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Vectorising nested for loops

조회 수: 1 (최근 30일)
Vishnu
Vishnu 2013년 12월 1일
마감: MATLAB Answer Bot 2021년 8월 20일
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..

답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by