Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 599
o17t H1H' S'k Avatar asked Sep 04 '12 12:09

o17t H1H' S'k


People also ask

What is the most useful function of GNU Octave?

GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab.

How do you use octave in matrix?

Matrices are entered row by row using the same syntax as for vectors. To interchange rows with columns, that is, to find the transpose of a vector or a matrix, use the apostrophe. For example, the command octave#:#> C = [4 7.5 -1]' will transform the row vector C = [4 7.5 -1] into a column vector.

How do you find the diagonal of a octave matrix?

The most common and easiest way to create a diagonal matrix is using the built-in function diag. The expression diag (v) , with v a vector, will create a square diagonal matrix with elements on the main diagonal given by the elements of v , and size equal to the length of v .


3 Answers

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?

like image 62
Akash Avatar answered Oct 08 '22 21:10

Akash


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
like image 33
o17t H1H' S'k Avatar answered Oct 08 '22 22:10

o17t H1H' S'k


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

like image 2
Eric Leschinski Avatar answered Oct 08 '22 22:10

Eric Leschinski