hxd1011 hxd1011 - 1 year ago 83
R Question

How to vectorize expanding a compressed sparse matrix from vector of column indices?

I want to build a matrix from a vector as follow: if the first element of y is 5, I want to make the first row and 5th column of matrix 1. others in the row are 0.

for (i in 1:length(y)){

Is there any way to avoid the for loop? I was trying to do
but apparently it is not working.

Answer Source

Yes: use a two-column index matrix. From ?"[":

When indexing arrays by ‘[’ a single argument ‘i’ can be a matrix with as many columns as there are dimensions of ‘x’; the result is then a vector with elements corresponding to the sets of indices in each row of ‘i’.


y <- round(runif(30)*9)+1

Your way (I streamlined the matrix construction a bit):

y_m <- matrix(0,ncol=10,nrow=length(y))
for (i in 1:length(y)){
  y_m[i,y[i]] <- 1

Via matrix indexing:

y_m2 <- matrix(0,ncol=10,nrow=length(y))
y_m2[cbind(1:length(y),y)] <- 1


all.equal(y_m,y_m2)  ## TRUE
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download