How to compute distance between two vectors based on measures?












0















I want to compute the average of distances between each column and other columns in a matrix except itself.



I wrote this code:



For i=1:m
For j=1:m-1
If(i==j)
d=0;
Else
d=pdist2(a(:,i),a(:,j),'jaccard');
s=sum(d)/(m-1);
End
End
End


The matrix is nxm.



I know that pdist2 function support some distance measures like jaccard , cosine , hamming , euclidean....



But If I want to compute canberra or clark distance measures or measures like them, I should define a function in pdist2 this way:



D=pdist2(x,y,@distfun) 


I wanted to know how to define a distance function for these measures?





Edit: The aim is to then empirically obtain the point where the values start to decrease but the figures don't show this...



enter image description here










share|improve this question





























    0















    I want to compute the average of distances between each column and other columns in a matrix except itself.



    I wrote this code:



    For i=1:m
    For j=1:m-1
    If(i==j)
    d=0;
    Else
    d=pdist2(a(:,i),a(:,j),'jaccard');
    s=sum(d)/(m-1);
    End
    End
    End


    The matrix is nxm.



    I know that pdist2 function support some distance measures like jaccard , cosine , hamming , euclidean....



    But If I want to compute canberra or clark distance measures or measures like them, I should define a function in pdist2 this way:



    D=pdist2(x,y,@distfun) 


    I wanted to know how to define a distance function for these measures?





    Edit: The aim is to then empirically obtain the point where the values start to decrease but the figures don't show this...



    enter image description here










    share|improve this question



























      0












      0








      0








      I want to compute the average of distances between each column and other columns in a matrix except itself.



      I wrote this code:



      For i=1:m
      For j=1:m-1
      If(i==j)
      d=0;
      Else
      d=pdist2(a(:,i),a(:,j),'jaccard');
      s=sum(d)/(m-1);
      End
      End
      End


      The matrix is nxm.



      I know that pdist2 function support some distance measures like jaccard , cosine , hamming , euclidean....



      But If I want to compute canberra or clark distance measures or measures like them, I should define a function in pdist2 this way:



      D=pdist2(x,y,@distfun) 


      I wanted to know how to define a distance function for these measures?





      Edit: The aim is to then empirically obtain the point where the values start to decrease but the figures don't show this...



      enter image description here










      share|improve this question
















      I want to compute the average of distances between each column and other columns in a matrix except itself.



      I wrote this code:



      For i=1:m
      For j=1:m-1
      If(i==j)
      d=0;
      Else
      d=pdist2(a(:,i),a(:,j),'jaccard');
      s=sum(d)/(m-1);
      End
      End
      End


      The matrix is nxm.



      I know that pdist2 function support some distance measures like jaccard , cosine , hamming , euclidean....



      But If I want to compute canberra or clark distance measures or measures like them, I should define a function in pdist2 this way:



      D=pdist2(x,y,@distfun) 


      I wanted to know how to define a distance function for these measures?





      Edit: The aim is to then empirically obtain the point where the values start to decrease but the figures don't show this...



      enter image description here







      matlab matrix euclidean-distance






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 9:50









      Wolfie

      17k51746




      17k51746










      asked Jan 2 at 21:59









      engineer100engineer100

      105




      105
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The Canberra Distance is defined as



          equation



          You are passing two column vectors into pdist2, so we can implement the above equation as an anonymous function which accepts to vector inputs...



          fCanberraDist = @(p,q) sum( abs(p - q) ./ ( abs(p) + abs(q) ) );


          Now you have the function handle, you can use the following:



          % ... previous code
          d = pdist2( a(:,i), a(:,j), fCanberraDist );
          % ... further code


          Note you're not currently doing anything with your result, I'm assuming that's just for the sake of the example code.



          You can define any other custom distance measure in a similar way, as either an anonymous function or a fully separate function (in its own .m file or defined locally).






          share|improve this answer
























          • After the loop, I want to use plot(s) to plot the m disrances and maybe I can find a threshold for these values.Is It practical to find a threshold from the figure of the values?or there is a better procedure?

            – engineer100
            Jan 4 at 8:00











          • It's unclear what you mean by "find a threshold" - what would be the criteria for this threshold? If you can define it mathematically then it's better to do programmatically.

            – Wolfie
            Jan 4 at 8:07











          • I want to empirically obtain the point where the values start to decrease but the figures don't show this, for example, I attached the figure of Canberra distances in my question. You mean that I should define It mathematically first.

            – engineer100
            Jan 4 at 9:00











          • I have answered your original question, I can't tell you why a figure you've produced doesn't show the results you expected... You've not given a Minimal, Complete, and Verifiable example to reproduce these results, or any explanation as to why they aren't as expected? This perhaps needs its own question, if the original question here about implementing a custom distance measure has been answered. Please mark my answer as accepted if this is the case.

            – Wolfie
            Jan 4 at 10:21














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013724%2fhow-to-compute-distance-between-two-vectors-based-on-measures%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The Canberra Distance is defined as



          equation



          You are passing two column vectors into pdist2, so we can implement the above equation as an anonymous function which accepts to vector inputs...



          fCanberraDist = @(p,q) sum( abs(p - q) ./ ( abs(p) + abs(q) ) );


          Now you have the function handle, you can use the following:



          % ... previous code
          d = pdist2( a(:,i), a(:,j), fCanberraDist );
          % ... further code


          Note you're not currently doing anything with your result, I'm assuming that's just for the sake of the example code.



          You can define any other custom distance measure in a similar way, as either an anonymous function or a fully separate function (in its own .m file or defined locally).






          share|improve this answer
























          • After the loop, I want to use plot(s) to plot the m disrances and maybe I can find a threshold for these values.Is It practical to find a threshold from the figure of the values?or there is a better procedure?

            – engineer100
            Jan 4 at 8:00











          • It's unclear what you mean by "find a threshold" - what would be the criteria for this threshold? If you can define it mathematically then it's better to do programmatically.

            – Wolfie
            Jan 4 at 8:07











          • I want to empirically obtain the point where the values start to decrease but the figures don't show this, for example, I attached the figure of Canberra distances in my question. You mean that I should define It mathematically first.

            – engineer100
            Jan 4 at 9:00











          • I have answered your original question, I can't tell you why a figure you've produced doesn't show the results you expected... You've not given a Minimal, Complete, and Verifiable example to reproduce these results, or any explanation as to why they aren't as expected? This perhaps needs its own question, if the original question here about implementing a custom distance measure has been answered. Please mark my answer as accepted if this is the case.

            – Wolfie
            Jan 4 at 10:21


















          0














          The Canberra Distance is defined as



          equation



          You are passing two column vectors into pdist2, so we can implement the above equation as an anonymous function which accepts to vector inputs...



          fCanberraDist = @(p,q) sum( abs(p - q) ./ ( abs(p) + abs(q) ) );


          Now you have the function handle, you can use the following:



          % ... previous code
          d = pdist2( a(:,i), a(:,j), fCanberraDist );
          % ... further code


          Note you're not currently doing anything with your result, I'm assuming that's just for the sake of the example code.



          You can define any other custom distance measure in a similar way, as either an anonymous function or a fully separate function (in its own .m file or defined locally).






          share|improve this answer
























          • After the loop, I want to use plot(s) to plot the m disrances and maybe I can find a threshold for these values.Is It practical to find a threshold from the figure of the values?or there is a better procedure?

            – engineer100
            Jan 4 at 8:00











          • It's unclear what you mean by "find a threshold" - what would be the criteria for this threshold? If you can define it mathematically then it's better to do programmatically.

            – Wolfie
            Jan 4 at 8:07











          • I want to empirically obtain the point where the values start to decrease but the figures don't show this, for example, I attached the figure of Canberra distances in my question. You mean that I should define It mathematically first.

            – engineer100
            Jan 4 at 9:00











          • I have answered your original question, I can't tell you why a figure you've produced doesn't show the results you expected... You've not given a Minimal, Complete, and Verifiable example to reproduce these results, or any explanation as to why they aren't as expected? This perhaps needs its own question, if the original question here about implementing a custom distance measure has been answered. Please mark my answer as accepted if this is the case.

            – Wolfie
            Jan 4 at 10:21
















          0












          0








          0







          The Canberra Distance is defined as



          equation



          You are passing two column vectors into pdist2, so we can implement the above equation as an anonymous function which accepts to vector inputs...



          fCanberraDist = @(p,q) sum( abs(p - q) ./ ( abs(p) + abs(q) ) );


          Now you have the function handle, you can use the following:



          % ... previous code
          d = pdist2( a(:,i), a(:,j), fCanberraDist );
          % ... further code


          Note you're not currently doing anything with your result, I'm assuming that's just for the sake of the example code.



          You can define any other custom distance measure in a similar way, as either an anonymous function or a fully separate function (in its own .m file or defined locally).






          share|improve this answer













          The Canberra Distance is defined as



          equation



          You are passing two column vectors into pdist2, so we can implement the above equation as an anonymous function which accepts to vector inputs...



          fCanberraDist = @(p,q) sum( abs(p - q) ./ ( abs(p) + abs(q) ) );


          Now you have the function handle, you can use the following:



          % ... previous code
          d = pdist2( a(:,i), a(:,j), fCanberraDist );
          % ... further code


          Note you're not currently doing anything with your result, I'm assuming that's just for the sake of the example code.



          You can define any other custom distance measure in a similar way, as either an anonymous function or a fully separate function (in its own .m file or defined locally).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 9:29









          WolfieWolfie

          17k51746




          17k51746













          • After the loop, I want to use plot(s) to plot the m disrances and maybe I can find a threshold for these values.Is It practical to find a threshold from the figure of the values?or there is a better procedure?

            – engineer100
            Jan 4 at 8:00











          • It's unclear what you mean by "find a threshold" - what would be the criteria for this threshold? If you can define it mathematically then it's better to do programmatically.

            – Wolfie
            Jan 4 at 8:07











          • I want to empirically obtain the point where the values start to decrease but the figures don't show this, for example, I attached the figure of Canberra distances in my question. You mean that I should define It mathematically first.

            – engineer100
            Jan 4 at 9:00











          • I have answered your original question, I can't tell you why a figure you've produced doesn't show the results you expected... You've not given a Minimal, Complete, and Verifiable example to reproduce these results, or any explanation as to why they aren't as expected? This perhaps needs its own question, if the original question here about implementing a custom distance measure has been answered. Please mark my answer as accepted if this is the case.

            – Wolfie
            Jan 4 at 10:21





















          • After the loop, I want to use plot(s) to plot the m disrances and maybe I can find a threshold for these values.Is It practical to find a threshold from the figure of the values?or there is a better procedure?

            – engineer100
            Jan 4 at 8:00











          • It's unclear what you mean by "find a threshold" - what would be the criteria for this threshold? If you can define it mathematically then it's better to do programmatically.

            – Wolfie
            Jan 4 at 8:07











          • I want to empirically obtain the point where the values start to decrease but the figures don't show this, for example, I attached the figure of Canberra distances in my question. You mean that I should define It mathematically first.

            – engineer100
            Jan 4 at 9:00











          • I have answered your original question, I can't tell you why a figure you've produced doesn't show the results you expected... You've not given a Minimal, Complete, and Verifiable example to reproduce these results, or any explanation as to why they aren't as expected? This perhaps needs its own question, if the original question here about implementing a custom distance measure has been answered. Please mark my answer as accepted if this is the case.

            – Wolfie
            Jan 4 at 10:21



















          After the loop, I want to use plot(s) to plot the m disrances and maybe I can find a threshold for these values.Is It practical to find a threshold from the figure of the values?or there is a better procedure?

          – engineer100
          Jan 4 at 8:00





          After the loop, I want to use plot(s) to plot the m disrances and maybe I can find a threshold for these values.Is It practical to find a threshold from the figure of the values?or there is a better procedure?

          – engineer100
          Jan 4 at 8:00













          It's unclear what you mean by "find a threshold" - what would be the criteria for this threshold? If you can define it mathematically then it's better to do programmatically.

          – Wolfie
          Jan 4 at 8:07





          It's unclear what you mean by "find a threshold" - what would be the criteria for this threshold? If you can define it mathematically then it's better to do programmatically.

          – Wolfie
          Jan 4 at 8:07













          I want to empirically obtain the point where the values start to decrease but the figures don't show this, for example, I attached the figure of Canberra distances in my question. You mean that I should define It mathematically first.

          – engineer100
          Jan 4 at 9:00





          I want to empirically obtain the point where the values start to decrease but the figures don't show this, for example, I attached the figure of Canberra distances in my question. You mean that I should define It mathematically first.

          – engineer100
          Jan 4 at 9:00













          I have answered your original question, I can't tell you why a figure you've produced doesn't show the results you expected... You've not given a Minimal, Complete, and Verifiable example to reproduce these results, or any explanation as to why they aren't as expected? This perhaps needs its own question, if the original question here about implementing a custom distance measure has been answered. Please mark my answer as accepted if this is the case.

          – Wolfie
          Jan 4 at 10:21







          I have answered your original question, I can't tell you why a figure you've produced doesn't show the results you expected... You've not given a Minimal, Complete, and Verifiable example to reproduce these results, or any explanation as to why they aren't as expected? This perhaps needs its own question, if the original question here about implementing a custom distance measure has been answered. Please mark my answer as accepted if this is the case.

          – Wolfie
          Jan 4 at 10:21






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013724%2fhow-to-compute-distance-between-two-vectors-based-on-measures%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith