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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:27:43+00:00 2026-05-26T19:27:43+00:00

The MATLAB function, pdist , computes pairwise distances for a set of points. In

  • 0

The MATLAB function, pdist, computes pairwise distances for a set of points. In order to save space and time, it elides repeat ( dist(A,B) == dist(B,A) ) and self ( dist(A,A) == 0 ) comparisons. The result is returned in a single vector, with indices [1 2 3 4 5 6 7 8 9 10]. Expanding into a full distance matrix ( e.g. by the function squareform ), the positions are mapped like:

-  -  -  -  -
1  -  -  -  -    # "1" is subscript "1,2"
2  5  -  -  -    
3  6  8  -  -    # "6" is subscript "4,2"
4  7  9  10 -

Given a vector such as that produced by pdist, what is the expression to convert an index into that vector into the subscript indices of the full distance matrix?

For example, I wish to find, in a set of points, the two points most distant from each other:

d = pdist(points); %# points is M x 2 (or 3, etc.), d is described above
N = length(d);
[~,I] = max(d); %# I is the index, e.g. 6
CI = ceil( sqrt( 2*N ) ) - round( sqrt( 2*(1 + N - I) ) ); %# Identity of one point, e.g. 4 is I == 6
RI = ????  %# Identity of second point, e.g. 2, if I == 6 and CI == 4

As indicated above, I have the first part, to find the first subscript. I can’t quite figure out the second one. There are a number of related questions (vide infra) but the answers aren’t correct (or are specific to 0-index, row-major languages).

  • algorithm for index numbers of triangular matrix coefficients
  • Mapping elements in 2D upper triangle and lower triangle to linear structure
  • 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-26T19:27:44+00:00Added an answer on May 26, 2026 at 7:27 pm

    @reve_etrange I believe N should be the length of d, not the number of rows of your points matrix.

    K=5; % Number of points
    d = pdist(rand(K,3));
    N = length(d);
    m = ceil(sqrt(2*N));
    I = 1:N;
    CI = m - round( sqrt( 2*(1 + N - I) ) );
    RI = mod(I + CI.*(CI+1)/2 - 1, m) + 1;
    

    This results in

    I =    1     2     3     4     5     6     7     8     9    10
    RI =   2     3     4     5     3     4     5     4     5     5
    CI =   1     1     1     1     2     2     2     3     3     4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know Matlab has a function called cylinder to create the points for a
I have a MATLAB function that finds charateristic points in a sample. Unfortunatley it
How can I convert this script into a MATLAB function? clear all; set={AB02XY_1,ZT99ER_1,UI87GP_1}; fileData1
I am finding that a lot of time spent in my matlab function is
Is there a matlab function to initiate a program at a preset time? for
I am plotting a financial candlestick chart using this MATLAB function: http://www.mathworks.com/help/toolbox/finance/candlefts.html How do
I have discovered through trial and error that the MATLAB engine function is not
I'm trying to embed Python into a MATLAB mex function on OS X. I've
I have a function in MATLAB which takes another function as an argument. I
Is there an open-source alternative to MATLAB's fmincon function for constrained linear optimization? I'm

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.