英文:
compute the minimum L1 distance of rows between two matrices in julia
问题
我有两个矩阵
A = {1,2,3,3,2,2;
2,2,2,2,3,1;
2,2,2,2,2,2}
B = {2,2,2,2,3,1;
2,2,2,2,2,2;
1,2,3,3,2,2}
我想计算每行在A
和B
之间的最小L1距离/城市街区距离,使得
minimum_distance_function(A,B)
= 0
我实际上有一个800x39的矩阵,不确定如何快速计算这个最小距离!
英文:
I have two matrices
A = {1,2,3,3,2,2;
2,2,2,2,3,1;
2,2,2,2,2,2}
B = {2,2,2,2,3,1;
2,2,2,2,2,2;
1,2,3,3,2,2}
I want to compute the minimum L1 distance/city block distance for each of the rows between A
and B
, such that
minimum_distance_function(A,B)
= 0
The dimensions for the matrices I have are actually 800x39 and I'm not sure how to compute this minimum distance quickly!
答案1
得分: 1
使用 Distances
A = [1 2 3 3 2 2;
2 2 2 2 3 1;
2 2 2 2 2 2]
B = [2 2 2 2 3 1;
2 2 2 2 2 2;
1 2 3 3 2 2]
findmin(pairwise(Cityblock(), A', B'))
英文:
using Distances
A = [1 2 3 3 2 2;
2 2 2 2 3 1;
2 2 2 2 2 2]
B = [2 2 2 2 3 1;
2 2 2 2 2 2;
1 2 3 3 2 2]
findmin(pairwise(Cityblock(), A', B'))
The last statement returns:
julia> findmin(pairwise(Cityblock(), A', B'))
(0, CartesianIndex(2, 1))
where 0
is the minimum and CartesianIndex(2,1)
is a pair of rows which obtain it.
This solution may not scale well to too many rows, in that case, splitting the work into row blocks of one matrix will reduce memory demands (and is a little more involved).
Finally, for really big instances, some sort of hierarchical clustering may be the way to go. There is much literature on such problems. But the simple solution above should suffice, or even just a double for
loop on rows would do.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论