For loop

3 views (last 30 days)
harjan
harjan on 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 Comments
Andrei Bobrov
Andrei Bobrov on 16 Aug 2011
your cod with example
harjan
harjan on 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......

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 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 Comment
harjan
harjan on 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

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 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 Comment
harjan
harjan on 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......

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!