cumsum(matC1)#累计和
c1 <- c(1:3)# 1,2,3
cumsum(c1)#return a vector whose element is the cumulative sum. 1, 3, 6
cummax(c1)#累计最大值
c1 <- c(1,3,2,4)
cummax(c1) # 1 3 3 4
按分布选择数
#功能:返回已知分布下,随机选择数的索引值。
drnd <-function(n, prob){
u<-runif(n)
cup<-cumsum(prob) #累加和,为了划分区间
#z<-x[as.integer(cut(u,breaks=c(0,cup)))]
z <- as.integer(cut(u,breaks=c(0,cup))) #cut函数返回:给定的每个数值所落在的区间,转化为整数后得到索引值
z
}
i_t <- drnd(1, p) //p 为给定的概率向量
i_t
最简单方法
data = c(1,2,3,4)
proba = c(.05,.1,.05,.8)
a_sampled = sample(data,100,replace = T, prob = proba)
a_sampled
生成与另外一个向量等长的全零向量
x1 <- c(1,2,4,53,3)
x2 <- numeric(length(x1))
x2 //0 0 0 0 0