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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:06:32+00:00 2026-06-13T22:06:32+00:00

The following matrix a represents a from-to distance: a = [0 3 5 6;

  • 0

The following matrix a represents a from-to distance:

a = [0 3 5 6; 4 0 2 9; 9 1 0 4; 8 3 8 0];

a =

 0     3     5     6
 4     0     2     9
 9     1     0     4
 8     3     8     0

(I.e. From 2 To 4 = 9, From 1 To 2 = 3, etc.)

matrix a is static(not changing) and there is another matrix b which changes. An example input is:

b = [1; 4; 2; 1; 3; 4; 1];

b =

 1
 4
 2
 1
 3
 4
 1

I am looking to find the distance between the first row and the second row — and perform that task for all rows to output:

b =

 1  
 4  6
 2  3
 1  4
 3  5
 4  4
 1  8

Where the second column is the value obtained from matrix a.

  • 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-13T22:06:33+00:00Added an answer on June 13, 2026 at 10:06 pm

    Here is a one line solution:

    %#Set parameters of problem
    a = [0 3 5 6; 4 0 2 9; 9 1 0 4; 8 3 8 0];
    b = [1; 4; 2; 1; 3; 4; 1];
    aSize = size(a);
    
    %#The one-line solution 
    Soln = [b, [NaN; a(sub2ind(aSize, b(1:end-1), b(2:end)))]];
    

    Alternatively, the non-vectorized solution is:

    %#Set parameters of problem
    a = [0 3 5 6; 4 0 2 9; 9 1 0 4; 8 3 8 0];
    b = [1; 4; 2; 1; 3; 4; 1];
    T = length(b);
    
    %#Preallocate a distance vector
    bDist = NaN(T, 1);
    
    %#Loop over b getting the distance for each pair
    for t = 2:T
        bDist(t, 1) = a(b(t-1), b(t));
    end
    
    %#Combine b and bDist into a single matrix.
    Soln = [b bDist];
    

    So which solution is optimal? On my machine (R2012b, Linux Mint v12), the tipping-point length of b is about 650. If b is larger than 650, then use the vectorized solution (ie the one-liner). If b is smaller than 650, then use the loop.

    By the way, if you are wondering, the one-line solution works by converting the successive pairs in b into row and column indices for the matrix a by using the sub2ind function. The solution vector is then found by indexing a using these indices.

    ps An asymmetric distance matrix? Sounds like an interesting problem!

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

Sidebar

Related Questions

In R I have the following matrix (each row represents a bootstrap 95% confidence
Is there a easy way how to produce following matrix: a = 4 5
Say I have the following matrix which is vertical : {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} And I want
I have the following matrix of data, which I am reading into SAS: 1
I have an array of arrays P, which represents a Matrix, as an array
I have the following code which is an implementation of BPM (bipartite matching, from
suppose I have the following matrix a = 2 NaN NaN 4 NaN 3
Say I have the following matrix: A = randi(10, [6 3]) 7 10 3
I have this following numpy matrix that I want to sort in ascending order
Say I have the following similarity matrix : matrix = [[100.0, 66.666666666666671, 61.539999999999999, 59.260000000000005,

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.