help me to reduce my function time runing

조회 수: 4 (최근 30일)
mostafa
mostafa 2014년 10월 21일
댓글: mostafa 2014년 10월 21일
Hi every body
This is my code
if true
for r = 1:100
U = 1 * (r-1);
for c = 1:100
V = 2 * (c-1);
F(r,c) = 10 * sin(U + V);
end
end
imagesc(F);
colormap(gray);
end
Its generate a replicate pattern which I upload it here. Is there any suggestion to reduce the running time for this code?
Thanks

채택된 답변

Roger Stafford
Roger Stafford 2014년 10월 21일
U = (0:99).';
V = 0:2:198;
F = (10*sin(U))*cos(V)+(10*cos(U))*sin(V);
imagesc(F);
colormap(gray);

추가 답변 (1개)

Henrik
Henrik 2014년 10월 21일
편집: Henrik 2014년 10월 21일
This should work:
if true
F=zeros(100,100); %always preallocate
c = 1:100;
V = 2 * (c-1);
for r = 1:100
U = 1 * (r-1);
F(r,:) = 10 * sin(U + V);
end
imagesc(F);
colormap(gray);
end
You can get rid of the r loop too with e.g. bsxfun, but I don't have time to test it right now
  댓글 수: 1
mostafa
mostafa 2014년 10월 21일
your answer is true but if i change the size to 150*100 it wouldn't work.

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

카테고리

Help CenterFile Exchange에서 Waveform Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by