R - 矩阵中的循环

发布于 2024-12-31 21:27:40 字数 1140 浏览 1 评论 0原文

我有两个变量,第一个是包含 230 个数据的一维流向量,第二个是二维温度矩阵(230*44219)。

我试图找到每个流量值和相应的 44219 温度之间的相关矩阵。这是我下面的代码。

Houlgrave_flow_1981_2000 = window(Houlgrave_flow_average, start = as.Date("1981-11-15"),end = as.Date("2000-12-15")) 

> str(Houlgrave_flow_1981_2000)
‘zoo’ series from 1981-11-15 to 2000-12-15
Data: num [1:230] 0.085689 0.021437 0.000705 0 0.006969 ...
Index:  Date[1:230], format: "1981-11-15" "1981-12-15" "1982-01-15" "1982-02-15" ...

Hulgrave_SST_1981_2000=X_sst[1:230,]

> str(Hulgrave_SST_1981_2000)
num [1:230, 1:44219] -0.0733 0.432 0.2783 -0.1989 0.1028 ...

sf_Houlgrave_SF_SST = NULL
sst_Houlgrave_SF_SST = NULL
cor_Houlgrave_SF_SST = NULL
for (i in 1:230) {
     for(j in 1:44219){
          sf_Houlgrave_SF_SST[i] =  Houlgrave_flow_1981_2000[i]
          sst_Houlgrave_SF_SST[i,j] = Hulgrave_SST_1981_2000[i,j]
          cor_Houlgrave_SF_SST[i,j] = cor(sf_Houlgrave_SF_SST[i],Hulgrave_SST_1981_2000[i,j]) 
     }
}

错误消息总是说:

Error in sst_Houlgrave_SF_SST[i, j] = Hulgrave_SST_1981_2000[i, j] : 
  incorrect number of subscripts on matrix

谢谢您的帮助。

I have two variables, the first is 1D flow vector containing 230 data and the second is 2D temperature matrix (230*44219).

I am trying to find the correlation matrix between each flow value and corresponding 44219 temperature. This is my code below.

Houlgrave_flow_1981_2000 = window(Houlgrave_flow_average, start = as.Date("1981-11-15"),end = as.Date("2000-12-15")) 

> str(Houlgrave_flow_1981_2000)
‘zoo’ series from 1981-11-15 to 2000-12-15
Data: num [1:230] 0.085689 0.021437 0.000705 0 0.006969 ...
Index:  Date[1:230], format: "1981-11-15" "1981-12-15" "1982-01-15" "1982-02-15" ...

Hulgrave_SST_1981_2000=X_sst[1:230,]

> str(Hulgrave_SST_1981_2000)
num [1:230, 1:44219] -0.0733 0.432 0.2783 -0.1989 0.1028 ...

sf_Houlgrave_SF_SST = NULL
sst_Houlgrave_SF_SST = NULL
cor_Houlgrave_SF_SST = NULL
for (i in 1:230) {
     for(j in 1:44219){
          sf_Houlgrave_SF_SST[i] =  Houlgrave_flow_1981_2000[i]
          sst_Houlgrave_SF_SST[i,j] = Hulgrave_SST_1981_2000[i,j]
          cor_Houlgrave_SF_SST[i,j] = cor(sf_Houlgrave_SF_SST[i],Hulgrave_SST_1981_2000[i,j]) 
     }
}

The error message always says:

Error in sst_Houlgrave_SF_SST[i, j] = Hulgrave_SST_1981_2000[i, j] : 
  incorrect number of subscripts on matrix

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2025-01-07 21:27:41

试试这个:

# prepare empty matrix of correct size
cor_Houlgrave_SF_SST <- matrix(nrow=dim(Hulgrave_SST_1981_2000)[1],
                              ncol=dim(Hulgrave_SST_1981_2000)[2])

# Good practice to not specify "230" or "44219" directly, instead
for (i in 1:dim(Hulgrave_SST_1981_2000)[1]) {
  for(j in 1:dim(Hulgrave_SST_1981_2000)[2]){
     cor_Houlgrave_SF_SST[i,j] <- cor(sf_Houlgrave_SF_SST[i],Hulgrave_SST_1981_2000[i,j]) 
   }
}

我认为循环内的两个重新定义是多余的。您的代码的主要问题是没有定义矩阵 - 即 cor 变量没有二维,因此会出现错误。
通过提前显式地给出正确的维度来为 for 循环中的结果定义空矩阵显然也是一种很好的做法 - 旨在使代码更加高效。

try this:

# prepare empty matrix of correct size
cor_Houlgrave_SF_SST <- matrix(nrow=dim(Hulgrave_SST_1981_2000)[1],
                              ncol=dim(Hulgrave_SST_1981_2000)[2])

# Good practice to not specify "230" or "44219" directly, instead
for (i in 1:dim(Hulgrave_SST_1981_2000)[1]) {
  for(j in 1:dim(Hulgrave_SST_1981_2000)[2]){
     cor_Houlgrave_SF_SST[i,j] <- cor(sf_Houlgrave_SF_SST[i],Hulgrave_SST_1981_2000[i,j]) 
   }
}

The two redefinitions inside your loop were superfluous I believe. The main problem with your code was not defining the matrix - i.e. the cor variable did not have 2 dimensions, hence the error.
It is apparently also good practice to define empty matrices for results in for-loops by explicitly giving them correct dimensions in advance - is meant to make the code more efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文