Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Vectorisation of a transform
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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?
0 comentarios
Respuestas (2)
  Image Analyst
      
      
 el 14 de En. de 2017
        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.
0 comentarios
  Andrei Bobrov
      
      
 el 14 de En. de 2017
        
      Editada: Andrei Bobrov
      
      
 el 14 de En. de 2017
  
      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;
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


