J.R.
J.R.

Reputation: 185

How to inverse matrix and integer result in Octave?

I would like to get an invertible matrix in Octave but as integers matrix, so:

x = [9,15;19,2];
inv(x)

Here I get:

[-0.0074906, 0.0561798; 0.0711610, -0.0337079]

but I would like to get [22,17;25,21] anyone knows how to invert a matrix?

Upvotes: 7

Views: 40040

Answers (3)

Jordan Miller
Jordan Miller

Reputation: 21

I'm very late to this and don't know how to answer the question efficiently, but it looks like you're looking to find the modular inverse of the matrix, in particular mod 26.

x = [9,15,19,2];
modulus = 26;
inverse_determinant = mod_inverse(det(x),modulus)

You have to implement the mod_inverse function by yourself, but the algorithm should be easy enough to find. If this is only for small modulus values, then a linear search should be efficient enough.

result = mod(det(x)*inv(x)*inverse_determinant,modulus)`

Upvotes: 2

Eric Leschinski
Eric Leschinski

Reputation: 154023

Invert a matrix in octave:

You are confused about what an inverse of a matrix is, don't nobody here knows what you want with your output, so here are some clues.

If you Invert an identity matrix, you get the identity matrix:

octave:3> a = [1,0;0,1]
a =

   1   0
   0   1

octave:4> inv(a)
ans =

   1   0
   0   1

Non-square matrices (m-by-n matrices for which m != n) do not have an inverse

x = [9,15;19,2;5,5]; 
inv(x) 
%error: inverse: argument must be a square matrix

Inverting a matrix with a zero on the diagonal causes an infinity:

octave:5> a = [1,0;0,0]
a =

   1   0
   0   0

octave:6> inv(a)
warning: inverse: matrix singular to machine precision, rcond = 0
ans =

   Inf   Inf
   Inf   Inf

Inverting a matrix with full values like this:

octave:1> a = [1,2;3,4]
a =
   1   2
   3   4

octave:2> inv(a)
ans =    
  -2.00000   1.00000
   1.50000  -0.50000

For a description of what is happening under the hood of the inverse function:

https://www.khanacademy.org/math/precalculus/precalc-matrices/inverting_matrices/v/inverse-of-a-2x2-matrix

Upvotes: 9

Jader Dias
Jader Dias

Reputation: 90515

The inverse of each element is:

x .^ -1

Which results

0.1111    0.0667
0.0526    0.5000

Why you want to get [22,17;25,21]? What mathematical operation would yield such result?

Upvotes: 14

Related Questions