计算两个矩阵之间行的最小L1距离在Julia中。

huangapple go评论45阅读模式
英文:

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}

我想计算每行在AB之间的最小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.

huangapple
  • 本文由 发表于 2023年4月19日 23:15:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定