Anybody knows how can i optimize this? Thanks <3!
function filter()
r=[cos(c) -sin(c);
sin(c) cos(c)];
for i = 1 : n2
for j = 1 : n1
h(i,j) = magic(r(1),std)*magic(r(2),std);
end
end
function y = magic(r,s)
y = exp(-r^2));

 채택된 답변

Roger Stafford
Roger Stafford 2015년 4월 9일

2 개 추천

You have set std1 and std2 to equal values. Provided you do that, the result in 'h' is independent of the value 'c' and can be vectorized as:
n1 = 10;
n2 = 3;
s = 2
h = exp(-(((1-n2)/2:(n2-1)/2)').^2/(2*s^2))*...
exp(-(((1-n1)/2:(n1-1)/2)).^2/(2*s^2))/s^2/(2*pi);
The code can also be vectorized with unequal values of std1 and std2, but is somewhat more complicated. Do you wish to see that?

추가 답변 (1개)

amelia_3
amelia_3 2015년 4월 9일
편집: amelia_3 2015년 4월 9일

0 개 추천

Thank you!! :) Actually, I'm trying to make it with different values too. However, I don't understand your solution neither :/

댓글 수: 1

Roger Stafford
Roger Stafford 2015년 4월 10일
편집: Roger Stafford 2015년 4월 10일
For unequal std1 and std2 a vectorized form would be:
[J,I] = meshgrid((1-n1)/2:(n1-1)/2,(1-n2)/2:(n2-1)/2);
cc = cos(c); sc = sin(c);
h = exp(-(cc*J-sc*I).^2/(2*std1^2)-(sc*J+cc*I).^2/(2*std2^2))...
/(std1*std2*2*pi);
To show that if std1 = std2, then the result is independent of c, the argument of exp above becomes:
-(cc*J-sc*I).^2/(2*std^2)-(sc*J+cc*I).^2/(2*std^2) =
(-(cc^2+sc^2)*J^2+(2*cc*sc-2*sc*cc)*I*J-(sc^2+cc^2)*I^2)/(2*std^2) =
-(J^2+I^2)/(2*std^2)
which is indeed independent of c.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 4월 9일

편집:

2015년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by