Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:50:56+00:00 2026-05-25T00:50:56+00:00

I can’t remove this loops by myself, if anybody can help me with optimization

  • 0

I can’t remove this loops by myself, if anybody can help me with optimization this piece of code – welcome.

M = length(w);
expt = exp(-t .* (phis * w'));
G = zeros(M, M);
for i = 1 : M
    for j = 1 : M
        last = 2 * reg_coef * eye(M);
        G(i,j) = last(i, j) + mean(expt .* t .^2 .* phis(:,i) .* phis(:,j) ./ (1 + expt) .^ 2);
    end
end
  • w – size is (1xM)
  • phis – size is (NxM)
  • t – size is (Nx1)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:50:56+00:00Added an answer on May 25, 2026 at 12:50 am

    You can do alot better by just writing cleaner code – ensure that you allocate properly, ensure that you remove duplicate calculations from loops etc:

    EDIT: You can also see that the resulting matrix G is symmetric, so you get an immediate 2x speed-up by only calculating the upper triangle and filling in the lower triangle afterwards as a transpose.

    At least with my MATLAB another big speed-up is achieved by vectorising the call to mean() through the use of a temporary array.

    N = 100;
    M = 100;
    
    w = rand(1,M);
    t = rand(N,1);
    phis = rand(N,M);
    reg_coeff = rand(1,1);
    
    expt = exp(-t .* (phis * w'));
    
    %% Initial version
    tic
    for i = 1 : M
       for j = 1 : M
          last = 2 * reg_coeff * eye(M,M);
          G1(i,j) = last(i,j) + mean(expt .* t .^ 2 .* phis(:,i) .* phis(:,j) ./ (1 + expt) .^ 2);
       end
    end
    t1 = toc;
    
    %% Faster version
    tic
    coeff = expt .* t .^ 2 ./ (1 + expt) .^ 2;
    G2 = zeros(M,M);
    TT = zeros(M,M);
    for i = 1 : M
       for j = i : M % only form upper triangle
          TT(:,j) = coeff .* phis(:,i) .* phis(:,j);
       end
       G2(i,i:M) = mean(TT(:,i:M),1); % vectorise call to mean()
    end
    G2 = 2 * reg_coeff * eye(M,M) + G2 + triu(G2,+1)';
    t2 = toc;
    
    %% Compare versions
    speed = t1/t2
    error = max(max(abs(G2 - G1)))
    

    For this 100x100 case the speed-up was around 41.0 on my machine.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody help me? What should be the datatype for this type -07:00:00 of
Can somebody help me convert this VB function to Perl (PHP, Ruby or Python
Can i get the source code for a WAMP stack installer somewhere? Any help
Can anyone help me trying to find out why this doesn't work. The brushes
can any one tell me how can change this java code into objective c.is
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Can anyone help me as what is going wrong here? Am not able to
Can anyone tell me how to open Auto CAD file in HTML5? Is this
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.