digit matrices
digit matrices
As an artificial but cute example, consider the determinants of
matrices
where each entry is a
non-negative integer in the range
, that is a digit.
The problem is to find the determinants,
, of all possible
matrices of this form and represent the frequency with which each value
occurs as a high density plot. This amounts to finding the
probability distribution of the determinant if each digit is chosen
independently and uniformly at random.
A neat way of doing this uses the outer() function twice:
> d <- outer(0:9, 0:9)
> fr <- table(outer(d, d, "-"))
> plot(as.numeric(names(fr)), fr, type="h",
xlab="Determinant", ylab="Frequency")
Notice the coercion of the names attribute of the frequency table to
numeric in order to recover the range of the determinant values. The
``obvious'' way of doing this problem with for-loops, to be
discussed in §It is also perhaps surprising that about 1 in 20 such matrices is singular.