Effacer les filtres
Effacer les filtres

For loop

1 vue (au cours des 30 derniers jours)
harjan
harjan le 16 Août 2011
hi 2 all, Can anyone say how to reduce execution time of 'for loop' in matlab How to replace for loop?
  2 commentaires
Andrei Bobrov
Andrei Bobrov le 16 Août 2011
your cod with example
harjan
harjan le 16 Août 2011
Thx a lot Here is my coding Pls say how to Change this "for loop"
[x y]=size(ima); %ima is 1600x1600 image
for i=1:x
for j=1:y
x(i,j)=sign(ima(i,j));
u1(i,j)=255*x(i,j);
...
...
end
end
It takes some amount of time to execute ....How to replace this Tell your suggestions......

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 16 Août 2011
u1 = 255*sign(ima);
but you get the array size is 1600 x 1600, with values of -255, 0 and 255
ADD on comments
if i1 and j1 integers from 1 to 1600, we obtain an array of 1600 by 1600, consisting of ones, in it case use
i1 = linspace(0,1,6);
j1 = i1;
T=cos(2*pi*repmat(i1',1,numel(j1))).*cos(2*pi*repmat(j1,numel(i1),1)); % 1 variant
T = bsxfun(@times,cos(2*pi*i1'),cos(2*pi*j1)); % 2 variant
T = cos(2*pi*i1')*cos(2*pi*j1);% 3 variant
  1 commentaire
harjan
harjan le 16 Août 2011
But how to change if this is in for loop
T(i,j)=(cos(2*pi*i)) * (cos(2*pi*j)); %for i,j varies from 1 to 1600
Thx in advance

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 16 Août 2011
The one sure way to reduce the execution time of a for loop is to eliminate that section of code.
Anything else depends on exactly what your for loop contains. There are some for loops that are now faster than vectorizing or using one of the library routines -- faster even than using a mex routine.
  1 commentaire
harjan
harjan le 16 Août 2011
Thx a lot Here is my coding Pls say how to Change this "for loop"
[x y]=size(ima); %ima is 1600x1600 image
for i=1:x
for j=1:y
x(i,j)=sign(ima(i,j));
u1(i,j)=255*x(i,j);
...
...
end
end
It takes some amount of time to execute ....How to replace this Tell your suggestions......

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by