why below code takes longer time to execute, even with only one input image it takes longer time to execute.
조회 수: 1 (최근 30일)
이전 댓글 표시
We plan to use this code for 1000 images so it is very difficult to complete so any other way is there to do this.
댓글 수: 0
채택된 답변
Guillaume
2018년 4월 4일
You haven't vectorised any of your code. Try this:
function [Fcc,Fcs,Fsc,Fss]=fun_cosine_series2(f,u,v)
a = pi * u / size(f, 1);
b = pi * v / size(f, 2);
rows = (1:size(f, 1)).';
cols = 1:size(f, 2);
cos1 = cos(a * (rows + 0.5));
cos2 = cos(b * (cols + 0.5));
sin1 = sin(a * (rows + 0.5));
sin2 = sin(b * (cols + 0.5));
Fcc = sum(sum(f .* cos1 .* cos2));
Fcs = sum(sum(f .* cos1 .* sin2));
Fsc = sum(sum(f .* sin1 .* cos2));
Fss = sum(sum(f .* sin1 .* sin2));
end
It requires R2016b or later for implicit expansion, if you're on an earlier version replace all the sum(sum(f .* x .* y)) by
sum(sum(f .* bsxfun(@times, x, y)))
where x and y are the respective cos an sin vectors obviously.
댓글 수: 1
Stephen23
2018년 4월 4일
For more information on code vectorization, and how to write efficient MATLAB code:
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!