How does GNU Octave matrix division work? Getting unexpected behaviour.

15,633

Solution 1

this is a answer i got from Alan Boulton at the coursera machine learning course discussion forum:

The gist of the idea is that x / y is defined quite generally so that it can deal with matrices. Conceptually the / operator is trying to return x∗y−1 (or x * inv(y) in Octave-speak), as in the following example:

octave:1> eye(2)/[1 2;3 4]
ans =
  -2.00000   1.00000
   1.50000  -0.50000

octave:2> inv([1 2;3 4])
ans =
  -2.00000   1.00000
   1.50000  -0.50000

The trickiness happens when y is a column vector, in which case the inv(y) is undefined, so pinv(y), the psuedoinverse of y, is used.

octave:1> pinv([1;2])
ans =
   0.20000   0.40000

octave:2> 1/[1;2]
ans =
   0.20000   0.40000

The vector y needs to be compatible with x so that x * pinv(y) is well-defined. So it's ok if y is a row vector, so long as x is compatible. See the following Octave session for illustration:

octave:18> pinv([1 2])
ans =
   0.20000
   0.40000

octave:19> 1/[1 2]
error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x2)
octave:19> eye(2)/[1 2]
ans =
   0.20000
   0.40000

octave:20> eye(2)/[1;2]
error: operator /: nonconformant arguments (op1 is 2x2, op2 is 2x1)
octave:20> 1/[1;2]
ans =
   0.20000   0.40000

Solution 2

Consider the following example:

>> A = [5 10];

>> B = [2 2];

If you want element-wise division, use A ./ B with the matrix size of both elements equal i.e If A is of size m∗n B must be of size m∗n

>> A ./B
ans =

    2.5000   5.0000

If you want matrix by matrix division, use A / B with the matrix size of element A as m∗n and B as q∗n or m∗n. The / operator is trying to return x∗y−1 (i.e x * pinv(y) in octave format).

>> A / B
ans =  3.7500

which is same as

>> A * pinv(B)
ans =  3.7500

pinv() function in OCTAVE/MATLAB returns the Moore-Penrose pseudo inverse of matrix, whereas the inv() function returns the inverse of the matrix. If you are confused about what to use, use pinv() If you want further clarification about What is the difference between pinv and inv?

Solution 3

Matrix division with Octave explained:

A formal description of Octave Matrix Division from here

http://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html

x / y
    Right division. This is conceptually equivalent to the expression
    (inverse (y') * x')'

    But it is computed without forming the inverse of y'.

    If the system is not square, or if the coefficient matrix is 
    singular, a minimum norm solution is computed. 

What that means is that these two should be the same:

[3 4]/[4 5; 6 7]
ans =
   1.50000  -0.50000

(inverse([4 5; 6 7]') * [3 4]')'
ans =
   1.50000  -0.50000

First, understand that Octave matrix division is not commutative, just like matrix Multiplication is not commutative.

That means A / B does not equal B / A

1/[1;1]
ans =
   0.50000   0.50000

[1;1]/1
ans =
   1
   1

One divided by a matrix with a single value one is one:

1/[1]
ans = 1

One divided by a matrix with a single value three is 0.33333:

1/[3]
ans = .33333

One divided by a (1x2) matrix:

1/[1;1]
ans =
   0.50000   0.50000

Equivalent:

([1/2;1/2] * 1)'
ans =
   0.50000   0.50000

Notice above, like the instructions said, we are taking the norm of the vector. So you see how the [1;1] was turned into [1/2; 1/2]. The '2' comes from the length of the vector, the 1 comes from the supplied vector. We'll do another:

One divided by a (1x3) matrix:

1/[1;1;1]
ans = 
    0.33333   0.33333   0.33333   

Equivalent:

 ([1/3;1/3;1/3] * 1)'
 ans =
    0.33333   0.33333   0.33333

What if one of the elements are negative...

1/[1;1;-1]
ans =
    0.33333   0.33333  -0.33333

Equivalent:

([1/3;1/3;-1/3] * 1)'
ans =
   0.33333   0.33333  -0.33333

So now you have a general idea of what Octave is doing when you don't supply it a square matrix. To understand what Octave matrix division does when you pass it a square matrix you need to understand what the inverse function is doing.

I've been normalizing your vectors by hand, if you want octave to do them you can add packages to do so, I think the following package will do what I've been doing with the vector normalization:

http://octave.sourceforge.net/geometry/function/normalizeVector.html

So now you can convert the division into an equivalent multiplication. Read this article on how matrix multiplication works, and you can backtrack and figure out what is going on under the hood of a matrix division.

http://www.purplemath.com/modules/mtrxmult2.htm

Share:
15,633
o17t H1H' S'k
Author by

o17t H1H' S'k

Personal pronouns and copulas (PPACs): Kindly use the Laadan male suffix -id when you are referring to meid and a male marker is lacking. e.g. you->youid, your->yourid, yours->yoursid, are->areid. If you are referring to a group of people of which Id amid part, but the others do not adhere to such pronouns or copulas, kindly explicitly exclude meid from that group and reconjunct meid separately using myid ppacs. e.g. "they are my friends" -> either "theyid areid my friends" or "they, excluding him, are my friends, and he isid my friend". Also i may use a smallcaps "i" as an abbreviation for "Id". Editing myid i's to I's as often happens is super-offensive. "You know what is reputation? Is people talking, is gossip. I also have reputation; not so pleasant, I think you know."

Updated on June 26, 2022

Comments

  • o17t H1H' S'k
    o17t H1H' S'k almost 2 years

    In GNU Octave, How does matrix division work?

    Instead of doing

    1./[1;1]
    

    I accidentally did

    1/[1;1]
    

    To my surprise this yields:

    [0.5, 0.5]
    

    The transverse case:

    1/[1,1]
    

    gives the expected:

    error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x2)
    

    Can someone explain the [0.5, 0.5] result?