I want to create a function that will make an $n\times n$ matrix which is non-singular that will always retrieve the same matrix (meaning that the matrix will not be random each time).

I tried using the table function with different expressions using $i$ and $j$ but they always ended up singular because they had similar rules.

So please don't give me a random matrix, the matrix needs to have a pattern that if two people used the function on different computers with the same $n$ value, they would get the same matrix

P.S. Keep the matrix in the Real set of numbers (also Integers) and don't make them too high so that its not too big to work with (I want the values to be ideally under 20)

  • 4
    $\begingroup$ I couldn't resist: IdentityMatrix[n] $\endgroup$
    – Kuba
    Aug 17, 2013 at 9:25
  • $\begingroup$ hmm yeah thats not gonna work, I'm doing Matrix Cryptography and I am going to pretend that the person I send the encrypted matrix knows the encoding matrix and using the Identity Matrix to encode it, won't be that encrypted. $\endgroup$
    – VikeStep
    Aug 17, 2013 at 9:27
  • $\begingroup$ I know it is not what are you looking for but it fits the question :) that's I couldn't resist to post it :) $\endgroup$
    – Kuba
    Aug 17, 2013 at 9:28
  • $\begingroup$ true true, well thanks, I'll update it in the OP, but the matrix should be only Integers, so Nassers answer won't work $\endgroup$
    – VikeStep
    Aug 17, 2013 at 9:30
  • $\begingroup$ I am confused. You say but the matrix should be only Integers, so Nassers answer won't work but HankelMatrix is all integers. May be you meant should NOT be integers? $\endgroup$
    – Nasser
    Aug 17, 2013 at 9:36

4 Answers 4


A non-singular matrix is a matrix with full rank. You can use any orthogonal basis, for example:

Table[HermiteH[i, j], {i, 5}, {j, 5}]

For a 5x5 non-singular matrix. There are several more basis generating function in Mathematica.

Since these numbers can be a bit big, here's a way of generating non-singular, non-random matrices with entries between -1 and 1:

orthMatrix[n_] := Orthogonalize[RandomReal[1, {n, n}]]

To make it non-random we use SeedRandom:

SeedRandom[1337]; orthMatrix[10]
  • $\begingroup$ wow, that has HUGH condition number! MatrixConditionNumber[ Table[HermiteH[i, j], {i, 5}, {j, 5}]] // N gives 431620. en.wikipedia.org/wiki/Condition_number that will cause about 5 decimal point accuracy loss? $\endgroup$
    – Nasser
    Aug 17, 2013 at 9:17
  • 1
    $\begingroup$ @Nasser We don't really know what the requirements for the matrix is. I'm just pointing out that HermiteH, LaguerreL, BesselJ etc. all solves the problem in a straightforward manner. $\endgroup$
    – C. E.
    Aug 17, 2013 at 9:23
  • $\begingroup$ Sure. But high condition number will cause numerical problems when used for Ax=b problems. But there are many ways to generate random matrices that are not singular. just googled it. So, fixing the seed to fixed value before starting the random number generator will cause the same matrix to be generated each time. $\endgroup$
    – Nasser
    Aug 17, 2013 at 9:26
  • 2
    $\begingroup$ @Nasser Added such a method. $\endgroup$
    – C. E.
    Aug 17, 2013 at 9:41
  • $\begingroup$ Using your last method with the orthMatrix, my values are all decimals, is there a way to make them integers under a maximum value? $\endgroup$
    – VikeStep
    Aug 17, 2013 at 9:56

maybe Hankel?

N[LinearAlgebra`MatrixConditionNumber[HankelMatrix[#]]] & /@ Range[4, 32]

{4.09288, 5.13808, 6.11956, 7.14252, 8.1322, 9.14612, 10.1395, \
11.1489, 12.1443, 13.151, 14.1477, 15.1527, 16.1502, 17.1541, \
18.1521, 19.1552, 20.1536, 21.1562, 22.1548, 23.157, 24.1559, \
25.1577, 26.1567, 27.1583, 28.1574, 29.1588, 30.1581, 31.1593, \
  • $\begingroup$ I updated it in the OP, but I forgot to mention that all values are integers :/ thanks anyways $\endgroup$
    – VikeStep
    Aug 17, 2013 at 9:33
  • $\begingroup$ but it IS all integers. That what Hankel matrix is. The above is NOT the matrix, these are the conditions numbers. Showing the matrix is stable numerically. $\endgroup$
    – Nasser
    Aug 17, 2013 at 9:37
  • $\begingroup$ oh, ok, I tested it out in Mathematica and it seems like the best for my scenario so far, I will keep this option open $\endgroup$
    – VikeStep
    Aug 17, 2013 at 9:44

Just for fun:

n = 5;
Partition[Prime /@ Range[n^2], n]
{{2, 3, 5, 7, 11},
 {13, 17, 19, 23, 29}, 
 {31, 37, 41, 43, 47}, 
 {53, 59, 61, 67, 71}, 
 {73, 79, 83, 89, 97}}

a little improvement:

Prime@Array[Plus, {n, n}]
{{3, 5, 7, 11, 13}, 
 {5, 7, 11, 13, 17}, 
 {7, 11, 13, 17, 19}, 
 {11, 13, 17, 19, 23}, 
 {13, 17, 19, 23, 29}}

it will generate lower values because rows will overlap in n-1 posistions. Also I used the fact that Prime is listable.

  • $\begingroup$ Thanks, this is my favourite, and this will work for all values of n? $\endgroup$
    – VikeStep
    Aug 17, 2013 at 9:18
  • $\begingroup$ @user9053 it suppose to but for huge n it may be slow and generate huge values :) I don't have big experience with primes so you have to check if it is good for your purposes :) Please, do not accept so fast, the question is quite new, let's do not discourage others :) you can still upvote if you like it $\endgroup$
    – Kuba
    Aug 17, 2013 at 9:21
  • $\begingroup$ High condition number also ! $\endgroup$
    – Nasser
    Aug 17, 2013 at 9:22
  • $\begingroup$ I'll unaccept the answer for the moment, and if it's the best answer later I'll accept it $\endgroup$
    – VikeStep
    Aug 17, 2013 at 9:32
  • $\begingroup$ @user9053 Ok ;). Also, take a look at my edit, it will generate lower values. $\endgroup$
    – Kuba
    Aug 17, 2013 at 9:34

OK, so I decided on a method similar to Anon's solution

I am just going to use


However I wanted to find an optimal seed for my scenario. I generally wanted to have all values of $n$ from 1 - 20 so I made the following program to find it out


1918, 3381, 3385

$\therefore$ My seed value should work for those values.

If anyone refers back to this and needs to make it work for every value of $n$, I recommend any of the other three options as they are all excellent


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.