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 Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by