Hey, Im trying to run this code but the run time is too long. can anyone help me out here plz

조회 수: 1 (최근 30일)
This function that I wrote is supposed to take the DCT of the input image. The run time is too much which I expect to be because of number of irritations. The picture that im using has 700*500 pixels. As u can see theres 4 nested for loops which makes the number of irritations to be 700*500*700*500 (almost 10^11). its that realy too many irritation for a computer or is there something wrong with the code? if yes what could it be?
function out_image = dct2(in_image)
s = size(in_image);
in_image = double(in_image);
out_image = zeros(s);
for i = 1:s(1)
for j = 1:s(2)
x = 0;
ci = 1;
cj = 1;
if i == 1
ci = sqrt(2)/2;
end
if j == 1
cj = sqrt(2)/2;
end
for m = 1:s(1)
for n = 1:s(2)
a = cos((((2*(m-1))+1)*(i-1)*pi)/(2*s(1)));
b = cos((((2*(n-1))+1)*(j-1)*pi)/(2*s(2)));
x = x + (a * b * in_image(m,n));
end
end
out_image(i,j) = (2/sqrt(s(1)*s(2)))* ci * cj * x;
end
end
end

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 3월 13일
편집: Mohammad Sami 2020년 3월 13일
As a starting point you can vectorize your inner for loops
m = repelem(1:s(1),1,s(2));
n = repmat(1:s(2),1,s(1));
a = cos((((2.*(m-1))+1).*(i-1).*pi)/(2*s(1)));
b = cos((((2.*(n-1))+1).*(j-1).*pi)/(2*s(2)));
ind = sub2ind(size(in_image),m,n);
x = sum(a.*.b.* in_image(ind));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by