Distance Matrix Without Loop
Afficher commentaires plus anciens
Hi all,
I would like to get a matrix A that for each aij, I'd get the distance (i.e. abs(x-y)) between the i and j values in the following manner:
x = [3, 5, 10];
y = [3, 5, 10]; % those are the locations in x and y, they are the same
Desired Matrix:
A = [0 2 7
2 0 5
7 5 0];
Please notice it's normal abs(x-y) and not the euclidean distance as technically both x and y are on the same axis.
I do know I can do a for loop to achieve this, but I might have a very large matrix and I was wondering if a quicker way is possible.
Thanks!
Réponses (1)
x = [3, 5, 10];
y = [3, 5, 10];
A = abs(x(:)-y(:).')
Catégories
En savoir plus sur Get Started with Optimization Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!