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