Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Vectorisation of a transform

조회 수: 3 (최근 30일)
Mat Hunt
Mat Hunt 2017년 1월 14일
마감: MATLAB Answer Bot 2021년 8월 20일
I am numerically coding up a transform with the following code:
function y=transform(x,f_x,z)
dx=x(2)-x(1);
n=length(z);
R=repmat(z',1,4);
y=zeros(1,n);
%Convert data to mid point
n_temp=length(x)-1;
x_mid=x(1:n_temp)+0.5*dx;
f_x_mid=zeros(1,n_temp);
for i=1:length(x_mid)
f_x_mid(i)=0.5*(f_x(i)+f_x(i+1));
end
for i=1:n
y(i)=trapz(x_mid,f_x_mid./(z(i)-x_mid))/pi;
end
Now it works pretty quickly when z is a vector of length 300, around 1 second. If I am doing this a large number of times then this will take a long time. Is there any easy way of speeding this up?

답변 (2개)

Image Analyst
Image Analyst 2017년 1월 14일
There's no way it should take a second for 300 elements. What did you do? You forgot to show how you called the function. I just took a guess and did this:
x = 1 : 300;
f_x = cos(x/130)+1;
z = x;
tic
y=transform(x,f_x,z);
toc
and got an elapsed time of 0.007 seconds, like a hundred times faster than you. What did you do ???
It doesn't look like z, n, and R are needed at all so you might want to get rid of those variables.

Andrei Bobrov
Andrei Bobrov 2017년 1월 14일
편집: Andrei Bobrov 2017년 1월 14일
function y=transform(x,f_x,z)
dx=x(2)-x(1);
n=length(z);
%Convert data to mid point
n_temp=length(x)-1;
x_mid=x(1:n_temp)+0.5*dx;
f_x_mid=conv2(f_x(:),.5*[1;1],'valid');
y = trapz(x_mid(:),f_x_mid./(z(:)'-x_mid))/pi;

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by