简体   繁体   English

多维数组索引改变我的矩阵的值

[英]multidimensional array indexing changing values of my matrices

I've got a multidimensional array with 13,057 8X11 matrices inside it. 我有一个多维数组,里面有13,057个8X11矩阵。

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. 谢谢。

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. 当你在一个数组上调用head时,你没有找到第一个矩阵的头部,你正在获得向量的头部。 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. 玩上面的LL ,看看第一个元素, LL[[1]] ,seoncd, LL[[2]]等。

Have a look at the difference between LL[1, , ] and LL[, , 1] . 看看LL[1, , ]LL[, , 1] LL[1, , ]之间的区别。 What you may think of as "the first matrix" is not necessarily what R would consider such. 您可能认为“第一个矩阵”并不一定是R会考虑的。


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. 或者更确切地说,您正在分配一个更小(更暗淡)的阵列,而R正在回收。

Instead of results[] <- m , use results[i, , ] <- m 而不是results[] <- m ,使用results[i, , ] <- m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM