mardi 29 octobre 2019

Generate an Asymmetric NxM matrix whose Rows and Columns Independently Sum to 1

Given a target matrix size of N rows and M columns, is it possible to pick values such that all rows and columns sum to 1, on the condition that the matrix is not symmetric across the diagonal? Here's a target matrix I was able to generate when N==M (The problems arise when N!=M - see below):

[[0.08345877 0.12844672 0.90911941 0.41964704 0.57709569]
 [0.53949086 0.07965491 0.62582134 0.48922244 0.38357809]
 [0.80619328 0.27581426 0.31312973 0.26855717 0.4540732 ]
 [0.11803505 0.88201276 0.1990759  0.2818701  0.63677383]
 [0.57058968 0.75183898 0.07062126 0.6584709  0.06624682]]

I'm writing this in numpy. Currently I've written the following (brute force) code, which I know works when n==m. However, if n != m, rowwise and columwise sums don't converge to 0, and the ratio of rowwise sums to columwise sums converges to (n/m):

n,m = (5,4)
mat = np.random.random((n,m))
for i in range(100):
    s0 = mat.sum(0)
    s1 = mat.sum(1)[:,newaxis]
    mat = (mat/s0)
    mat = (mat/s1)
    if i%10 == 0:
        print(s0[0]/s1[0,0])

The final output in this case is 1.25 (I.e. n/m, or 5/4). I'm beginning to think this might not be mathematically possible. Can someone prove me wrong?




Aucun commentaire:

Enregistrer un commentaire