user3067923
user3067923

Reputation: 447

multidimensional array indexing changing values of my matrices

I've got a multidimensional array with 13,057 8X11 matrices inside it.

dim(results)
[1] 13057     8    11

Every time I to index a particular matrix, the values inside the matrix change. For example, say I do:

head(results)
[1] 0.2789366 0.1555067 0.3026808 0.1644631 0.2325074 0.3162973

but when I do

results[1,,]

          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]
[1,] 0.2789366 0.2789366 0.2789366 0.2789366 0.2789366 0.2789366 0.2789366 0.2789366 0.2789366
[2,] 0.1191516 0.1191516 0.1191516 0.1191516 0.1191516 0.1191516 0.1191516 0.1191516 0.1191516
[3,] 0.1805271 0.1805271 0.1805271 0.1805271 0.1805271 0.1805271 0.1805271 0.1805271 0.1805271
[4,] 0.1846702 0.1846702 0.1846702 0.1846702 0.1846702 0.1846702 0.1846702 0.1846702 0.1846702
[5,] 0.4004454 0.4004454 0.4004454 0.4004454 0.4004454 0.4004454 0.4004454 0.4004454 0.4004454
[6,] 0.1336974 0.1336974 0.1336974 0.1336974 0.1336974 0.1336974 0.1336974 0.1336974 0.1336974
[7,] 0.2155045 0.2155045 0.2155045 0.2155045 0.2155045 0.2155045 0.2155045 0.2155045 0.2155045
[8,] 0.5307715 0.5307715 0.5307715 0.5307715 0.5307715 0.5307715 0.5307715 0.5307715 0.5307715

Why are my all of my rows having the same value? This is NOT what the initial value of this matrix was before I put it in the multidimensional array nor is it the value I get when I print all of the matrices in the console.

This is my code:

range01 <- function(x){(x-min(x))/(max(x)-min(x))}
results = array(0,dim=c(13057,8,11))

for( i in 1:13057) {
  ap <- as.numeric(unlist(ap.dv[i,1:11]))
  dv <- as.numeric(unlist(ap.dv[i,12:19]))
  m <- outer(ap, dv, FUN="*")
  m[is.na(m)] <- 0
  m <- range01(m)
  results[] <- m
}

thanks.

Upvotes: 0

Views: 100

Answers (1)

Ricardo Saporta
Ricardo Saporta

Reputation: 55350

Under any array or matrix, is really just a vector.
When you call head on an array, you are not geting the head of the first matrix, you are getting the head of the vector. Look at the ordering of the letters below

> LL <- LETTERS[1:24]
> dim(LL) <- c(2, 3, 4)
> head(LL)
[1] "A" "B" "C" "D" "E" "F"

## Have a look at LL: 

> LL
, , 1

     [,1] [,2] [,3]
[1,] "A"  "C"  "E" 
[2,] "B"  "D"  "F" 

, , 2

     [,1] [,2] [,3]
[1,] "G"  "I"  "K" 
[2,] "H"  "J"  "L" 

, , 3

     [,1] [,2] [,3]
[1,] "M"  "O"  "Q" 
[2,] "N"  "P"  "R" 

, , 4

     [,1] [,2] [,3]
[1,] "S"  "U"  "W" 
[2,] "T"  "V"  "X" 

Play with LL above, have a look at the first element, LL[[1]], the seoncd, LL[[2]], etc.

Have a look at the difference between LL[1, , ] and LL[, , 1]. What you may think of as "the first matrix" is not necessarily what R would consider such.


As for your question of why you have repeated values, the answer is that you are actually assigning these values yourself. Or rather, you are assigning a smaller (less dim) array, and R is recycling.

Instead of results[] <- m, use results[i, , ] <- m

Upvotes: 2

Related Questions