The unknown matrix is like this:
> x<-matrix(c("x11","x12","x13","x21","x22","x23","x31","x32","x33"),nrow=3,ncol=3,byrow=TRUE)
> x
     [,1]  [,2]  [,3] 
[1,] "x11" "x12" "x13"
[2,] "x21" "x22" "x23"
[3,] "x31" "x32" "x33"

This is my distance matrix.
> D<-matrix(c(1,5,10,5,2,6,10,6,8),nrow=3,ncol=3,byrow=TRUE)
> D
     [,1] [,2] [,3]
[1,]    1    5   10
[2,]    5    2    6
[3,]   10    6    8

I know the sums of each row and column of the X-matrix and also the sumproducts of each row and column (x11*D11+x12*D12+x13*D13). My task is to find matrix X with non-negative elements matching the known sums and sumproducts. I tried to solve this task as a system of linear equations like this: 
1.x11+1.x12+1.x13+0.x21+0.x22+0.x23+0.x31+0.x32+0.x33 = [known sum of row 1]
0.x11+0.x12+0.x13+1.x21+1.x22+1.x23+0.x31+0.x32+0.x33 = [known sum of row 2]

0.x11+0.x12+0.x13+0.x21+0.x22+0.x23+1.x31+1.x32+1.x33 = [known sum of row 3]

1.x11+0.x12+0.x13+1.x21+0.x22+0.x23+1.x31+0.x32+0.x33 = [known sum of column 1]

0.x11+1.x12+0.x13+0.x21+1.x22+0.x23+0.x31+1.x32+0.x33 = [known sum of column 2]

0.x11+0.x12+1.x13+0.x21+0.x22+1.x23+0.x31+0.x32+1.x33 = [known sum of column 3]

D11.x11+D12.x12+D13.x13+0.x21+0.x22+0.x23+0.x31+0.x32+0.x33 = [known sumproduct of row 1 X[1,] and D[1,]]
0.x11+0.x12+0.x13+D21.x21+D22.x22+D23.x23+0.x31+0.x32+0.x33 = [known sumproduct of row 2]

0.x11+0.x12+0.x13+0.x21+0.x22+0.x23+D31.x31+D32.x32+D33.x33 = [known sumproduct of row 3]

D11.x11+0.x12+0.x13+D21.x21+0.x22+0.x23+D31.x31+0.x32+0.x33 = [known sumproduct of column 1]

0.x11+D12.x12+0.x13+0.x21+D22.x22+0.x23+0.x31+D32.x32+0.x33 = [known sumproduct of column 2]

0.x11+0.x12+D13.x13+0.x21+0.x22+D23.x23+0.x31+0.x32+D33.x33 = [known sumproduct of column 3]

In this case is overdetermined system, but in my case is underdetermined (matrix 28x28). I think I could use the "lsei" function but with some modification in the source that it would return only positive x-es, but I don't know where to put this check.

Thanks in advance.
Best regards.

	[[alternative HTML version deleted]]


