Results 1 to 4 of 4

Math Help - Help on Improving performance for MATLAB function

  1. #1
    Newbie
    Joined
    Mar 2009
    Posts
    6

    Help on Improving performance for MATLAB function

    Im trying to make my function run faster and I was wondering if anyone can help me. I'm trying to calculate the following equation :
    ImageShack - Image Hosting :: 29384513.jpg
    where N and M are inputs. Basically i need to caluclate that sum for all values of m.
    This is my current code:

    function [e] = new5(M,N)

    tic;

    k=3;
    n=(1:1:N);
    d=zeros(1,M-1);
    b=zeros(1,M-1);

    for m=1:1:M-1;
    d=m./M;
    b=cos((n.^k)*d);
    e(m)=(sum(b)/sqrt(N));
    end

    hold on
    %plot histogram of data
    hist(e,100);figure(gcf);
    hold off
    toc;

    Any ideas?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by thankarathan View Post
    Im trying to make my function run faster and I was wondering if anyone can help me. I'm trying to calculate the following equation :
    ImageShack - Image Hosting :: 29384513.jpg
    where N and M are inputs. Basically i need to caluclate that sum for all values of m.
    This is my current code:

    function [e] = new5(M,N)

    tic;

    k=3;
    n=(1:1:N);
    d=zeros(1,M-1);
    b=zeros(1,M-1);

    for m=1:1:M-1;
    d=m./M;
    b=cos((n.^k)*d);
    e(m)=(sum(b)/sqrt(N));
    end

    hold on
    %plot histogram of data
    hist(e,100);figure(gcf);
    hold off
    toc;

    Any ideas?
    Code:
    N=3;M=4;
     
    
    tic;
     
    
    k=3;
    n=(1:1:N);
    d=zeros(1,M-1);
    b=zeros(1,M-1);
     
    
    n=1:N;
    m=1:M-1;
     
    d=m/M;
    theta=n'.^k*d;
    b=cos(theta);
    e=sum(b)/sqrt(N);
     
    hold on
    
    plot histogram of data
    hist(e,100);figure(gcf);
    hold off
    
    toc
    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Mar 2009
    Posts
    6
    Thank you for your reply. Your code does indeed work a lot faster, however since I;m taking large values of N and M, I get an "out of memory" error for values of M,N> 5000
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by thankarathan View Post
    Thank you for your reply. Your code does indeed work a lot faster, however since I;m taking large values of N and M, I get an "out of memory" error for values of M,N> 5000
    Then you will have to segment the calculation over sub-blocks of M or N and loop over the blocks.

    Something like (this is untested by the way, so no guarantees):

    Code:
    N=3;M=4;
     
    
    tic;
     
    
    k=3;
    
    BlockLen=1000;
    e=zeros(1,M-1);
    
    m=1:M-1;
    
    for idx=1:1000:N
    
      n=(idx:min(N,idx+BlockLen-1));
     
      d=m/M;
      theta=n'.^k*d;
      b=cos(theta);
      e=e+sum(b)/sqrt(N);
    
    end
     
    hold on
    
    plot histogram of data
    hist(e,100);figure(gcf);
    hold off
    
    toc
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Regression by RMS method: Improving fit.
    Posted in the Advanced Statistics Forum
    Replies: 1
    Last Post: February 25th 2010, 07:29 PM
  2. Improving Limit of detection from two sets of data
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: July 20th 2009, 11:05 AM
  3. graph, constant and improving...
    Posted in the Math Topics Forum
    Replies: 2
    Last Post: May 7th 2009, 05:57 AM
  4. Logarithmic Function - Performance Problem
    Posted in the Algebra Forum
    Replies: 1
    Last Post: May 5th 2009, 01:30 AM

Search Tags


/mathhelpforum @mathhelpforum