Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily visualize a matrix?

When doing matrix operations, I would like to be able to see what the results of my calculations are, at least to get a rough idea of the nature of the matrices going in and coming out of the operation.

How can I plot a matrix of real numbers, so that the x axis represents columns, the y represents rows, and the color or size of a point represents the cell value?

Ultimately, I would like to display multiple plots, e.g. the right and left hand sides of an equation.

Here is some example code:

a <- matrix(rnorm(100), ncol = 10)
b <- diag(1,10)
c <- a*b

par(mfrow = c(1,3))
plot.matrix.fn <- function(m) {
   #enter answer to this question here
}
lapply(list(a,b,c), plot.matrix.fn)

update: since posting this question, I found that there are some great examples here: What techniques exists in R to visualize a "distance matrix"?

like image 968
Abe Avatar asked Jul 31 '11 01:07

Abe


People also ask

How do you visualize a matrix?

To sum up, the action of a matrix is to move the entire grid. We can understand it by thinking about how it moves the unit vectors. We can visualize what this looks like by drawing a modified 2D grid. These ideas extend into three dimensions as well.

How do you visualize a matrix in Excel?

Switching to Matrix VisualizationClick on the Table. Click the DESIGN tab. Click Table in the Switch Visualization group. Select Matrix from the dropdown list.

What is matrix visualization?

Matrix visualization (Chen, 2002; Chen et al., 2004) is a graphical technique that can simultaneously explore the associations between thousands of subjects, variables, and their interactions, without needing to first reduce the dimensions of the data.


2 Answers

You could try something like (adjusting the parameters to your particular needs)

   image(t(m[nrow(m):1,] ), axes=FALSE, zlim=c(-4,4), col=rainbow(21))

producing something like

enter image description here

like image 164
Henry Avatar answered Oct 11 '22 07:10

Henry


See ?image for a single plot (note that row 1 will be at the bottom) and ?rasterImage for adding 1 or more representations to an existing plot. You may want to do some scaling or other transformation on the matrix first.

like image 20
Greg Snow Avatar answered Oct 11 '22 07:10

Greg Snow