Filter löschen
Filter löschen

For loop

1 Ansicht (letzte 30 Tage)
harjan
harjan am 16 Aug. 2011
hi 2 all, Can anyone say how to reduce execution time of 'for loop' in matlab How to replace for loop?
  2 Kommentare
Andrei Bobrov
Andrei Bobrov am 16 Aug. 2011
your cod with example
harjan
harjan am 16 Aug. 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......

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 16 Aug. 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 Kommentar
harjan
harjan am 16 Aug. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 16 Aug. 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 Kommentar
harjan
harjan am 16 Aug. 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......

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by