MATLAB Answers

find distances between vectors in a cell array

1 view (last 30 days)
That Guy
That Guy on 12 Nov 2020
Answered: Stephen Cobeldick on 12 Nov 2020
so i have a 15x2 cell array where each row represents two sets of coordinate points i need to find the distances between:
[1,1] [4,4]
[1,1] [10,10]
[1,1] [1.50000000000000,1.50000000000000]
[1,1] [10.5000000000000,10.5000000000000]
[1.50000000000000,1] [4,4]
[1.50000000000000,1] [10,10]
[1.50000000000000,1] [1.50000000000000,1.50000000000000]
[1.50000000000000,1] [10.5000000000000,10.5000000000000]
[4,4] [10,10]
[4,4] [1.50000000000000,1.50000000000000]
[4,4] [10.5000000000000,10.5000000000000]
[10,10] [1.50000000000000,1.50000000000000]
[10,10] [10.5000000000000,10.5000000000000]
[1.50000000000000,1.50000000000000] [10.5000000000000,10.5000000000000]
pdist doesnt seem to work for me in this case bc the elements arent character arrays or strings. How would i go about this?

  0 Comments

Sign in to comment.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 12 Nov 2020
"pdist doesnt seem to work for me in this case bc the elements arent character arrays or strings."
According to the pdist documentation its first input must be numeric floating point, i.e. double or single.
Storing that data in a cell array is rather awkward, it would probably be simpler in one/two numeric arrays. However you can still use pdist on your pairs, with the help of cellfun:
C = {...
[1,1],[10,10]
[1,1],[1.5,1.5]
[1,1],[10.5,10.5]
[1.5,1],[4,4]
[1.5,1],[10,10]
[1.5,1],[1.5,1.5]
[1.5,1],[10.5,10.5]
[4,4],[10,10]
[4,4],[1.5,1.5]
[4,4],[10.5,10.5]
[10,10],[1.5,1.5]
[10,10],[10.5,10.5]
[1.5,1.5],[10.5,10.5]};
F = @(varargin)pdist(vertcat(varargin{:}));
D = cellfun(F,C(:,1),C(:,2))
D = 13×1
12.7279 0.7071 13.4350 3.9051 12.3794 0.5000 13.0863 8.4853 3.5355 9.1924

  0 Comments

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!