楼主: half'
24355 8

[问答] 请教一个R语言的问题,报错subscript out of bounds [推广有奖]

  • 0关注
  • 0粉丝

高中生

50%

还不是VIP/贵宾

-

威望
0
论坛币
16 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
176 点
帖子
16
精华
0
在线时间
25 小时
注册时间
2012-8-14
最后登录
2019-2-15

楼主
half' 发表于 2015-12-8 00:15:10 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
以下是我的程序代码:
S<-matrix(100,10,13)
ran<-matrix(runif(12,0,1),10,12)
for(i in 2:13){
  for(j in 1:10){
    if(ran[i-1,j]<0.45){
      S[i,j]<-1.1*S[i-1,j]
    }else if(ran[i-1,j]<0.65){
        S[i,j]<-S[i-1,j]
    }else{
        S[i,j]<-0.9*S[i-1,j]
      }
  }
}

运行后报错:
Error in `[<-`(`*tmp*`, i, j, value = 130.45131) :
  subscript out of bounds

真心求助各位大神错在什么地方
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:script Bounds Bound scrip scri 程序

沙发
neuroexplorer 发表于 2015-12-8 00:48:00
As you can see you have:
length(2:13)
[1] 12

however:
dim(ran)
[1] 10 12

dim(S)
[1] 10 13

You see? i iterates from 2:13, which is larger than both dim(ran)[1] and dim(S)[1]  

I think you should be able to fix the code now.

已有 1 人评分论坛币 收起 理由
admin_kefu + 40 热心帮助其他会员

总评分: 论坛币 + 40   查看全部评分

藤椅
half' 发表于 2015-12-8 01:01:45
neuroexplorer 发表于 2015-12-8 00:48
As you can see you have:
length(2:13)
[1] 12
Thank you!
I think I put i and j at wrong places in my 'for sentences'. = =
But after I switched i and j, it remains wrong.
It says 'Error in if (ran[i - 1, j] < 0.45) { : argument is of length zero'.
Could you please tell me how to solve this new problem?
Thank you very much.

板凳
half' 发表于 2015-12-8 01:13:21
neuroexplorer 发表于 2015-12-8 00:48
As you can see you have:
length(2:13)
[1] 12
I know how to solve this problem now.
And this is the fixed code:
S<-matrix(100,10,13)
ran<-matrix(runif(12,0,1),10,12)
for(i in 1:10){
  for(j in 2:13){
    if(ran[i,j-1]<0.45){
      S[i,j]<-1.1*S[i,j-1]
    }else if(ran[i,j-1]<0.65){
        S[i,j]<-S[i,j-1]
    }else{
        S[i,j]<-0.9*S[i,j-1]
      }
  }
}

Thank you.

报纸
neuroexplorer 发表于 2015-12-8 01:21:02
Here you go:

S<-matrix(100,10,13)
ran<-matrix(runif(12,0,1),10,12)
for(j in 2:13){
  for(i in 1:10){
    if(ran[i,j-1]<0.45){
      S[i,j]<-1.1*S[i,j-1]
    }else if(ran[i,j-1]<0.65){
      S[i,j]<-S[i,j-1]
    }else{
      S[i,j]<-0.9*S[i,j-1]
    }
  }
}

地板
neuroexplorer 发表于 2015-12-8 01:25:47
also, you do not have to use loop here. You can use logic indexing, instead. The latter is a lot faster than loop.

suppose:

m <- matrix(runif(12), nrow = 3)
> m
          [,1]      [,2]      [,3]      [,4]
[1,] 0.2790982 0.8438885 0.7953321 0.2782153
[2,] 0.9958075 0.6737918 0.0568551 0.7749910
[3,] 0.1693278 0.3926015 0.8945651 0.7571608

m[m > 0.5]  = 1
> m
          [,1]      [,2]      [,3]      [,4]
[1,] 0.2790982 1.0000000 1.0000000 0.2782153
[2,] 1.0000000 1.0000000 0.0568551 1.0000000
[3,] 0.1693278 0.3926015 1.0000000 1.0000000

7
half' 发表于 2015-12-8 02:00:38
neuroexplorer 发表于 2015-12-8 01:25
also, you do not have to use loop here. You can use logic indexing, instead. The latter is a lot fas ...
Just start using R, so most of my questions maybe stupid in your opinion.
I'm sure your solution is much better than mine, but I can't quite understand it so far.
I'll try to make it.

Here is another question:
Now I got a 10*13 matrix, every row represents a time series (T=0-12) of the price of a stock, once stock price>=110 for 3 consecutive time steps, print that T, otherwise print 0 at the end of this time period.

And here is the code I tried. Although it's obviously wrong:
Tseries<-c(1:10)
T=0
for(i in 1:10){
  for(j in 1:13){
    if(S[i,j]>=110){T<-T+1
    }else{
        T<-0
      }Tseries[j]<-T
  }
}

What should I do to solve this question?
Thank you a lot.

8
neuroexplorer 发表于 2015-12-8 05:48:21
  1. high_run <- function(x, threshold) {
  2.   high <- x >= threshold
  3.   streak <- high[1]
  4.   for(h in high[2:length(high)]){
  5.     streak <- c(streak, streak[length(streak)]*h + h)
  6.   }
  7.   #run
  8.   return(streak)
  9. }


  10. high_run_start <- function(x, threshold, run){
  11.   match(run, high_run(x, threshold)) - run + 1
  12. }
复制代码


for example:
suppose x vector x: 133.1115 106.6949 148.7925 138.0290 111.3889 132.4088 149.2900 128.8314 138.4674 143.7794 140.4188 125.6814 143.2901
high_run(x, 110)
[1]  1  0  1  2  3  4  5  6  7  8  9 10 11

then you know tht start from fourth, the number are consecutively larger than 110  (notice he index + 1 is the position)


if you run:

  1. high_run_start(x, 110, 3)
复制代码

you got 3, which is the index for the position: 3 + 1 = 4. so you got it.

to get it work in matrix, it could not be more straightforward enough ....  


9
alecwf 发表于 2018-11-16 17:55:10
: argument is of length zero'.  近来总遇到

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-20 16:42