필터 지우기
필터 지우기

For loop

조회 수: 1 (최근 30일)
harjan
harjan 2011년 8월 16일
hi 2 all, Can anyone say how to reduce execution time of 'for loop' in matlab How to replace for loop?
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2011년 8월 16일
your cod with example
harjan
harjan 2011년 8월 16일
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......

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 8월 16일
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
harjan
harjan 2011년 8월 16일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 8월 16일
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
harjan
harjan 2011년 8월 16일
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......

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by