decrease num of for loops

Hi
Can you help me to write that 4 for loops with just 1 for loop?
I really need to speed up my program and that is the biggest problem.
for u=0:7,
for v=0:7,
tmp = 0;
for x=0:7,
for y=0:7,
tmp = tmp + vhod(y+1, x+1) * cosd( (2*x + 1)*u*180/16 ) * cosd( (2*y + 1)*v*180/16 );
end
end
izhod(v+1, u+1) = round(0.25 * C(u+1) * C(v+1) * tmp);
end
end

 Réponse acceptée

Matt Fig
Matt Fig le 3 Nov 2012
Modifié(e) : Matt Fig le 3 Nov 2012

0 votes

This should be much faster:
D = (2*(0:7).'+1)*180/16; % No need to do this over and over.
for u=0:7
for v=0:7
tmp = vhod.*(cosd(D*v) * cosd(D.'*u));
izhod2(v+1, u+1) = C(u+1) * C(v+1) * sum(tmp(:));
end
end
izhod2 = round(0.25 *izhod2); %compare to izhod... equal!

5 commentaires

Matt Fig
Matt Fig le 3 Nov 2012
Uros comments ( again as an answer!!)
"thanks, that's great. it works much faster.
can you help me out with this similar function (idct actually):
for x=0:7,
for y=0:7,
tmp = 0;
for u=0:7,
for v=0:7,
tmp = tmp + C(u+1) * C(v+1) * vhod(v+1, u+1) * cosd( (2*x + 1)*u*180/16 ) * cosd( (2*y + 1)*v*180/16 );
end
end
izhod(y+1, x+1) = ceil(0.25 * tmp);
end
end
that's the last thing i need to speed up in my program"
Matt Fig
Matt Fig le 3 Nov 2012
It looks so similar that I think you can go through the code I gave you and just apply what you learn.
Uros
Uros le 3 Nov 2012
i'm trying but no success for now.
getting error at line 9 (inner matrix dimensions must agree)
D1 = (0:7);
D2 = (1:8);
for u=0:7,
for v=0:7
tmp = vhod.*( cosd((2*u + 1)*D1*180/16) * cosd((2*v + 1)*D1*180/16) * C(D2) * C(D2.'));
izhod(v+1, u+1) = sum(tmp(:));
end
end
izhod = ceil(0.25 * izhod);
Uros
Uros le 3 Nov 2012
:( i can't do it, in this case there is also C related to u and v and i'm a bit confused.
btw this is the formula i need to speed up: http://shrani.si/f/3j/bC/gP6Ixdg/brez-naslova.png
with: C = [1 / sqrt(2), 1, 1, 1, 1, 1, 1, 1];
Uros
Uros le 3 Nov 2012
still no success
current code:
for u=0:7,
for v=0:7
tmp = vhod.*(cosd((2*u+1)*(0:7)*180/16) * cosd((2*v+1)*(0:7)'*180/16) ).*(C(1:8) * C(1:8)');
izhod(v+1, u+1) = sum(tmp(:));
end
end
izhod = ceil(0.25 * izhod);
:(

Connectez-vous pour commenter.

Plus de réponses (1)

José-Luis
José-Luis le 3 Nov 2012

0 votes

old_v = 0;
for ii = 1:8*8*8*8
[u v x y] = ind2sub([8 8 8 8],ii);
u = u-1; v = v-1; x = x-1; y = y-1;
if old_v ~= v
tmp = 0;
end
%do your stuff
old_v = v;
end

1 commentaire

Matt Fig
Matt Fig le 3 Nov 2012
Uros comments:
"hm i can't really understand how it works
now i have:
tmp = 0;
old_v = 0;
for ii = 1:8*8*8*8
[u v x y] = ind2sub([8 8 8 8],ii);
u = u-1; v = v-1; x = x-1; y = y-1;
if old_v ~= v
tmp = 0;
end
tmp = tmp + vhod(y+1, x+1) * cosd( (2*x + 1)*u*180/16 ) * cosd( (2*y + 1)*v*180/16 );
old_v = v;
end
but i don't know where and how to put that line:
izhod(v+1, u+1) = round(0.25 * C(u+1) * C(v+1) * tmp);"

Connectez-vous pour commenter.

Catégories

En savoir plus sur Historical Contests dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by