Making Pairs of Indices in Matrix- Pairwise Function

12 visualizaciones (últimos 30 días)
Andrea Lucia Alfonso
Andrea Lucia Alfonso el 16 de Jun. de 2020
Comentada: Matt J el 16 de Jun. de 2020
Hello,
I want to make a pairwise function in matlab simialar to how it is done in python. Or is there one already in matlab?
Overall, I am trying to return a list of pairs of indices in my matrix and then take the distance between each pair.
Any input would be greatly appreciated.
Thank you!

Respuesta aceptada

Matt J
Matt J el 16 de Jun. de 2020
Editada: Matt J el 16 de Jun. de 2020
It depends what Matlab data-type you consider to be the equivalent of a Python tuple. I tend to think of cell arrays as the equivalent of a tuple because cells can store sequences of mixed types, but matrix form is generally the form in Matlab that will support most mathematical operations you might want to do. Below, I generate a set of pairs in both matrix and cell array form.
n=4;
pairMatrix=[0:n-1;1:n].';
pairCell=num2cell(pairMatrix,2)
Overall, I am trying to return a list of pairs of indices in my matrix and then take the distance between each pair.
You mean, the Euclidean distances when each pair is viewed as a 2D coordinate? If you have the Statistics Toolbox, then this can be done using pdist2,
>> pdist2(pairMatrix,pairMatrix)
ans =
0 1.4142 2.8284 4.2426
1.4142 0 1.4142 2.8284
2.8284 1.4142 0 1.4142
4.2426 2.8284 1.4142 0
  4 comentarios
Andrea Lucia Alfonso
Andrea Lucia Alfonso el 16 de Jun. de 2020
Thank you so much! This definitley helped!
Matt J
Matt J el 16 de Jun. de 2020
You're quite welcome, but please Accept-click the answer to signify that you are satisfied with the solution.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by