Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fast fill 3D array/matrix in R with known index and value

may I ask a stupid question about fast assigning values to a big 3D matrix in R? I have x, y, z for location index and val for values to fill. The for loop takes so long, could you please help? Note that here I just simulated xyz and val

x <- replicate(1e4, sample(seq(1:100), size = 1))
y <- replicate(1e4, sample(seq(1:100), size = 1))
z <- replicate(1e4, sample(seq(1:200), size = 1))
val <- rnorm(1e4, 0, 1)

a <- array(numeric(),c(100, 100, 200))
for(ii in seq(from = 1, to = length(x), by = 1)){
    a[x[ii], y[ii], z[ii]] <- val[ii]
}

I tied the following mapply but it returns to me a vector

mapply(function(i, j, k, v) a[i, j, k] <- v, x, y , z, val)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do

a[cbind(x, y, z)] <- val

It’s difficult to show that this works on such a large example, so let’s demonstrate on a smaller scale with a 3 x 3 x 3 matrix:

a <- array(numeric(), c(3, 3, 3))

x <- c(3, 2, 1)
y <- c(1, 1, 1)
z <- c(1, 2, 3)
val <- c(1, 2, 3)

So these variables imply that we want a one in the third row of the first column of our first slice, a two in the second row of the first column of our second slice, and a three in the first row of the first column of our third slice:

a[cbind(x, y, z)] <- val

a
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   NA   NA   NA
#> [2,]   NA   NA   NA
#> [3,]    1   NA   NA
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   NA   NA   NA
#> [2,]    2   NA   NA
#> [3,]   NA   NA   NA
#> 
#> , , 3
#> 
#>      [,1] [,2] [,3]
#> [1,]    3   NA   NA
#> [2,]   NA   NA   NA
#> [3,]   NA   NA   NA

Created on 2022-02-24 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading