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 8969495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:35:56+00:00 2026-06-15T17:35:56+00:00

I am trying to optimize my code and am not sure how and if

  • 0

I am trying to optimize my code and am not sure how and if I would be able to vectorize this particular section??

for base_num = 1:base_length
    for sub_num = 1:base_length
    dist{base_num}(sub_num) = sqrt((x(base_num) - x(sub_num))^2 + (y(base_num) - y(sub_num))^2);
    end
end
  • 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-15T17:35:57+00:00Added an answer on June 15, 2026 at 5:35 pm

    The following example provides one method of vectorization:

    %# Set example parameters
    N = 10;
    X = randn(N, 1);
    Y = randn(N, 1);
    
    %# Your loop based solution
    Dist1 = cell(N, 1);    
    for n = 1:N
        for m = 1:N
            Dist1{n}(m) = sqrt((X(n) - X(m))^2 + (Y(n) - Y(m))^2);
        end
    end
    
    %# My vectorized solution
    Dist2 = sqrt(bsxfun(@minus, X, X').^2 + bsxfun(@minus, Y, Y').^2);
    Dist2Cell = num2cell(Dist2, 2);
    

    A quick speed test at N = 1000 has the vectorized solution running two orders of magnitude faster than the loop solution.

    Note: I’ve used a second line in my vectorized solution to mimic your cell array output structure. Up to you whether you want to include it or two combine it into one line etc.

    By the way, +1 for posting code in the question. However, two small suggestions for the future: 1) When posting to SO, use simple variable names – especially for loop subscripts – such as I have in my answer. 2) It is nice when we can copy and paste example code straight into a script and run it without having to do any changes or additions (again such as in my answer). This allows us to converge on a solution more rapidly.

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

Sidebar

Related Questions

I'm trying to optimize the performance of my code, but I'm not familiar with
i'm trying to optimize this code: foreach (string id in ids) { MyClass x
I'm trying to condense/improve/optimize this code cleaned = stringwithslashes cleaned = cleaned.replace(\\n, \n) cleaned
I'm trying to optimize my code using Neon intrinsics. I have a 24-bit rotation
I'm trying to micro-optimize my code at a very low level point in the
I'm trying to optimize my C++ code. I've searched the internet on using dynamically
I am trying to optimize this query as good as possible,but still i am
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I'm not pretty sure if this question is for here, but I want to
Basically I'm trying to optimize my jQuery code by unbinding elements anytime is necessary,

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.