find distances between vectors in a cell array
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Stephen23
2020-11-12
"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))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!