Skip to content

Instantly share code, notes, and snippets.

@isomorphisms
Created February 9, 2012 21:08
Show Gist options
  • Save isomorphisms/1783240 to your computer and use it in GitHub Desktop.
Save isomorphisms/1783240 to your computer and use it in GitHub Desktop.
How to generate patterned matrices in R and format then in LaTeX
> require(Hmisc)
> require(sfsmisc)
> k.3 <- matrix(
c(
-2, 1, 0,
-1, 2, -1,
0, -1, 2),
nrow=3, ncol=3
)
> k.3
[,1] [,2] [,3]
[1,] 2 -1 0
[2,] -1 2 -1
[3,] 0 -1 2
> mat2tex( k.3, stdout() )
\begin{tabular} {|c |c |c |}
\hline
2 & -1 & 0 \\ \hline
-1 & 2 & -1 \\ \hline
0 & -1 & 2 \\ \hline
\end{tabular}
> random.binary.seq <- sample(
c(0,0,0,1), # ~75% of the entries are 0
81 , # for a 9-by-9 matrix
replace=TRUE
)
> random.binary.mat <- matrix( random.binary.seq, nrow=9, ncol=9 )
> random.binary.mat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 0 0 1 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 1 0 0
[3,] 0 0 0 1 0 1 0 0 0
[4,] 0 0 0 0 0 0 0 0 1
[5,] 0 0 0 0 0 0 1 0 0
[6,] 0 0 1 0 1 0 0 1 1
[7,] 0 1 1 0 0 0 0 0 0
[8,] 0 0 1 0 0 0 0 0 0
[9,] 1 0 0 0 0 0 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment