MATLAB Answers

Matrix division problem on adding condition

1 view (last 30 days)
David Han
David Han on 27 Jan 2020
Commented: David Han on 1 Feb 2020
I have a math model, the math is A*B=C
A is a 1*m matrix; B is m*n matrix and C is a 1*n matrix.
B has condition is the sum of each row must ==1.
I wrote a test code to test it:
A=[100 230 30]
B=[1 0 0;1 0 0;0 0 1]
but unfortuatly, the result D is not equal to B. I have no idea how could I add the condition of B, and how to write the correct code. and also why D~=B ?
thank you


Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 28 Jan 2020
Edited: David Goodmanson on 1 Feb 2020
Hi David,
the solution is not immediate, but you did ask …
Take the example
A = [1 2 3]
C = [5 2 -1]
B = the matrix you provided
Consider first the possible solutions for B without the restriction that the rows of B each add up to 1.
A*B = C
B0 = A\C
You would like B0 = B but instead,
B0 = 0 0 0
0 0 0
1.6667 0.6667 -0.3333
A*B = C consists of three equations but B has nine unknowns, so B is far from unique. The B that you provided is one solution, and so is B0.
The range of possibilities is determined by the null space of A. In this case the null space consists of two columns
N = null(A)
N =
-0.5345 -0.8018
0.7745 -0.3382
-0.3382 0.4927
and by the definition of null space, A*(either of these columns) = 0, or equivalently A*N = 0. You can add any linear combination of these two columns to any column of B0 (different columns can have different linear combinations) and still have a solution. The most general solution is
B = B0 + N*[c1 c2 c3; c4 c5 c6] where [ … ] is 2x3 [1]
and c1 … c6 are arbitrary parameters. Then
A*B0 = C and A*N = 0 --> A*B = C
The most general B has six arbitrary parameters.
The restriction that the rows of B each add up to 1 complicates matters a bit. To begin with, there are going to be restrictions on A vs C.
In matrix form the condition that each row of B sums to 1 is
B*[1;1;1] = [1;1;1]
which is slightly confusing because the [1;1;1] on the left produces the sum of each row and the [1;1;1] on the right says that the row sums, are 1,1, and 1. Anyway,
A*B = C
A*B*[1;1;1] = C*[1;1;1]
A*[1;1;1] = C*[1;1;1]
so the sum of the elements of A has to equal the sum of the elements of C. If not, there is no solution. Suppose the sums are equal, as they are in the example. Multiplying [1] on the right by [1;1;1] leads to the requirement
[s1;s4] = N\( [1;1;1]-B0*[1;1;1] )
where s1 is the sum of c1,c2,c3 and s4 is the sum of c4,c5,c6.
That’s two more conditions, so the most general B has four independent parameters. That still leaves a lot of room to roam within B. With the appropriately chosen parameters, B will equal your original matrix B. The code below gives a solution for four randomly chosen parameters.
onz = [1 1 1]';
A = [1 2 3];
C = [5 2 -1];
B = [1 0 0; 1 0 0; 0 0 1];
B0 = A\C
N = null(A)
s14 = N\(onz - B0*onz) % this is [s1;s4]
r = randn(2,2);
c = [r (-sum(r,2)+s14)]
B = B0 + N*c
A*B % should equal C
B*onz % row sums of B, should equal [1 1 1]'


Show 2 older comments
David Han
David Han on 28 Jan 2020
Yes, David A and C are known, and I want to calculate matrix B How can I get the correct B solution?
David Goodmanson
David Goodmanson on 1 Feb 2020
Hi David, after I got a clearer idea of what is happening I modified the answer above.
David Han
David Han on 1 Feb 2020
Thank you David,
This is the best answer and really a good solution, I will use your way to modify my model.
Really appreciate your time!

Sign in to comment.

More Answers (0)

Sign in to answer this question.