hxd1011 - 2 months ago 10

R Question

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.

`y=round(runif(30)*9)+1`

y_m=matrix(rep(0,length(y)*10),ncol=10)

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

y_m[i,y[i]]=1;

}

Is there any way to avoid the for loop? I was trying to do

`y_m[,y]=1`

Answer

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’.

Setup:

```
set.seed(101)
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
```

Check:

```
all.equal(y_m,y_m2) ## TRUE
```

Source (Stackoverflow)

Comments