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

  • Home
  • SEARCH
  • 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 8741573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:20:25+00:00 2026-06-13T11:20:25+00:00

After having learned basic programming in Java, I have found that the most difficult

  • 0

After having learned basic programming in Java, I have found that the most difficult part of transitioning to MatLab for my current algorithm course, is to avoid loops. I know that there are plenty of smart ways to vectorize operations in MatLab, but my mind is so “stuck” in loop-thinking, that I am finding it hard to intuitively see how I may vectorize code. Once I am shown how it can be done, it makes sense to me, but I just don’t see it that easily myself. Currently I have the following code for finding the barycentric weights used in Lagrangian interpolation:

function w = barycentric_weights(x);
% The function is used to find the weights of the
% barycentric formula based on a given grid as input.

n = length(x);
w = zeros(1,n);

% Calculating the weights
for i = 1:n
    prod = 1;
    for j = 1:n
        if i ~= j
            prod = prod*(x(i) - x(j));
        end
    end
    w(i) = prod;
end
w = 1./w;

I am pretty sure there must be a smarter way to do this in MatLab, but I just can’t think of it. If anyone has any tips I will be very grateful :). And the only way I’ll ever learn all the vectorizing tricks in MatLab is to see how they are used in various scenarios such as above.

  • 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-06-13T11:20:26+00:00Added an answer on June 13, 2026 at 11:20 am

    I can see the appeal of vectorization, but I often ask myself how much time it actually saves when I go back to the code a month later and have to decipher all that repmat gibberish. I think your current code is clean and clear and I wouldn’t mess with it unless performance is really critical. But to answer your question here is my best effort:

    function w = barycentric_weights_vectorized(x)
        n = length(x);
        w = 1./prod(eye(n) + repmat(x,n,1) - repmat(x',1,n),1);
    end
    

    Hope that helps!

    And I am assuming x is a row vector here.

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

Sidebar

Related Questions

I come from a Java background, but I learned C++ after and have been
After having my last question answered , I have never see the preventDefault(); function
I consider myself an Android developer, after having created four apps and learned a
After having some problems with these two linker errors on SO , I have
After having learnt the hard way that shared variables are currently not guarded by
After having upgraded to XCode 4 im getting some linker warnings that Im a
Having recently learned Grand Central Dispatch, I've found multithreaded code to be pretty intuitive(with
After having some issues using OpenSSL and gSOAP, I have been looking into the
Recently i've switched to PHP 5.3+ and after that migration i learned that the
I have recently learned that you can use a neat switch statement with fallthrough

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.