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!

Antworten (1)

Matt J
Matt J am 14 Nov. 2020
Bearbeitet: Matt J am 14 Nov. 2020

0 Stimmen

x = [3, 5, 10];
y = [3, 5, 10];
A = abs(x(:)-y(:).')
A = 3×3
0 2 7 2 0 5 7 5 0

Kategorien

Gefragt:

am 14 Nov. 2020

Bearbeitet:

am 14 Nov. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by