In Matlab, there is a function named spy, which displays the structure of a sparse matrix. It creates a plot of the matrix' dimensions where each entry that has a nonzero value is colored. Is there an equivalent function in R?
image()
from Matrix is one option.
library(Matrix)
# Example from ?Matrix:::sparseMatrix
i <- c(1,3:8); j <- c(2,9,6:10); x <- 7 * (1:7)
A <- sparseMatrix(i, j, x = x)
print(A)
##8 x 10 sparse Matrix of class "dgCMatrix"
##[1,] . 7 . . . . . . . .
##[2,] . . . . . . . . . .
##[3,] . . . . . . . . 14 .
##[4,] . . . . . 21 . . . .
##[5,] . . . . . . 28 . . .
##[6,] . . . . . . . 35 . .
##[7,] . . . . . . . . 42 .
##[8,] . . . . . . . . . 49
image(A)
To get the output of spy()
in R, it takes a little bit more work.
In MATLAB (2011b):
spy()
h = gcf;
axObj = get(h, 'Children');
datObj = get(axObj, 'Children');
xdata = get(datObj,'XData');
ydata = get(datObj,'YData');
spyMat = [xdata; ydata];
csvwrite('spydat.csv',spyMat);
And in R:
library(Matrix)
spyData <- read.csv("spydat.csv")
spyMat <- t(sparseMatrix(spyData[1,],spyData[2,]))
image(spyMat)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With