如何在R中生成多个矩阵(How to generate multiple matrix in R)

编程入门 行业动态 更新时间:2024-10-28 06:23:57
如何在R中生成多个矩阵(How to generate multiple matrix in R)

我在R中得到了两个值列表

daily_max_car: (List 1) 21 21 22 22 22 22 21 daily_0.8: (List 2) 16 17 17 17 18 17 17

尝试在R-Studio中编写For循环,使用这两个列表中的一个值(逐个)生成多个矩阵。

这是我用来生成一个矩阵的代码!

Lambda <- 21 (From List 1) Mue <- 4 Rho <- Lambda/Mue N <- 16 (From List 2)

所有这四个参数都将用于“calculatewq”函数中。

calculatewq <- function(c) {....Some thing happening } ##Create Matrix matrix1 <- matrix(0,Lambda,4) matrix1[,1] <- 1:Lambda ### Create a column of matrix with repeated "N" rep.row<-function(x,y) {matrix(rep(x,each=y),nrow=y)} created_mar_1 <- rep.row(N,Lambda) car_n<- created_mar_1-matrix1[,1] created_mar_3 <- rep.row(69*60*24,Lambda) ## Add into Matrix for (i in 1:Lambda) {matrix1[i,2] <- calculatewq(i)[2] matrix1[i,3] <- calculatewq(i)[5] matrix1[,4] = car_n*created_mar_3}`

一旦我改变其中一个参数,它将生成一个新的矩阵。 因此,当我在Lambda和N中放入不同的值时,如何编写for循环以生成多个矩阵。

非常感谢! 桑普森

I have gotten two lists of values in R.

daily_max_car: (List 1) 21 21 22 22 22 22 21 daily_0.8: (List 2) 16 17 17 17 18 17 17

Trying to write a For Loop in R-Studio to generate multiple matrix by using the one of the values from these two lists (One by One).

Here is the code I have been using to generate one matrix!

Lambda <- 21 (From List 1) Mue <- 4 Rho <- Lambda/Mue N <- 16 (From List 2)

All of these four parameters will be used in the "calculatewq" Function.

calculatewq <- function(c) {....Some thing happening } ##Create Matrix matrix1 <- matrix(0,Lambda,4) matrix1[,1] <- 1:Lambda ### Create a column of matrix with repeated "N" rep.row<-function(x,y) {matrix(rep(x,each=y),nrow=y)} created_mar_1 <- rep.row(N,Lambda) car_n<- created_mar_1-matrix1[,1] created_mar_3 <- rep.row(69*60*24,Lambda) ## Add into Matrix for (i in 1:Lambda) {matrix1[i,2] <- calculatewq(i)[2] matrix1[i,3] <- calculatewq(i)[5] matrix1[,4] = car_n*created_mar_3}`

Once I change one of the parameters it will generate a new matrix. Thus, how can I write a for loop to generate multiple matrix while I am putting different value in Lambda and N.

Thank you so much! Sampson

最满意答案

我在calculatewq函数中删除了for循环。 请确保你需要一个for循环。

myfun <- function(Lambda, N, mu ) { # browser() var1 <- seq_len( Lambda ) var2 <- ( rep( N, each = Lambda) ) - var1 var3 <- rep( 69*60*24, each = Lambda ) var4 <- var2 * var3 fun_vals <- do.call( 'rbind', lapply( var1, function( x ) calculatewq( x, Lambda = Lambda, N = N, mu = mu ) ) ) mat <- matrix( NA, nrow = Lambda, ncol = mu ) mat[, 1] <- var1 mat[, 2] <- fun_vals[, 'Wq'] mat[, 3] <- fun_vals[, 'customer_serviced'] mat[, 4] <- var4 return(mat) } calculatewq <- function( x, Lambda, N, mu ) { # browser() Rho <- Lambda / mu p0_inv <- ( Rho^x * (1-(( Rho/x )^( N-x+1)))) / (factorial( x ) * ( 1 - ( Rho / x ) ) ) p0_inv <- p0_inv + ( Rho^x) / factorial( x ) P0 <- 1/p0_inv Lq <- ( Rho^(x+1)) * (1-((Rho/x)^(N-x+1))-((N-x+1)*(1-(Rho/x))*((Rho/x)^(N-x))))*P0/(factorial(x-1)*(x-Rho)^2) Wq <- 60*Lq/Lambda Ls <- Lq + Rho Ws <- 60*Ls/Lambda PN <- (Rho^N)*P0/(factorial(x)*x^(N-x)) customer_serviced <- (1 - PN)*100 a <- cbind( Lq, Wq, Ls, Ws, customer_serviced ) return(a) } mu <- 4 res <- Map( myfun, list( 21 ,21, 22, 22 ,22, 22 ,21 ), list( 16, 17, 17, 17, 18, 17 ,17 ), mu) head( res[[1]]) # [,1] [,2] [,3] [,4] # [1,] 1 42.184874 19.04762 1490400 # [2,] 2 38.241748 38.09526 1391040 # [3,] 3 33.339271 57.13862 1291680 # [4,] 4 26.014138 75.70348 1192320 # [5,] 5 16.339462 89.88989 1092960 # [6,] 6 9.121053 96.32498 993600

I removed for loop inside calculatewq function. Please make sure you needed a for loop in it.

myfun <- function(Lambda, N, mu ) { # browser() var1 <- seq_len( Lambda ) var2 <- ( rep( N, each = Lambda) ) - var1 var3 <- rep( 69*60*24, each = Lambda ) var4 <- var2 * var3 fun_vals <- do.call( 'rbind', lapply( var1, function( x ) calculatewq( x, Lambda = Lambda, N = N, mu = mu ) ) ) mat <- matrix( NA, nrow = Lambda, ncol = mu ) mat[, 1] <- var1 mat[, 2] <- fun_vals[, 'Wq'] mat[, 3] <- fun_vals[, 'customer_serviced'] mat[, 4] <- var4 return(mat) } calculatewq <- function( x, Lambda, N, mu ) { # browser() Rho <- Lambda / mu p0_inv <- ( Rho^x * (1-(( Rho/x )^( N-x+1)))) / (factorial( x ) * ( 1 - ( Rho / x ) ) ) p0_inv <- p0_inv + ( Rho^x) / factorial( x ) P0 <- 1/p0_inv Lq <- ( Rho^(x+1)) * (1-((Rho/x)^(N-x+1))-((N-x+1)*(1-(Rho/x))*((Rho/x)^(N-x))))*P0/(factorial(x-1)*(x-Rho)^2) Wq <- 60*Lq/Lambda Ls <- Lq + Rho Ws <- 60*Ls/Lambda PN <- (Rho^N)*P0/(factorial(x)*x^(N-x)) customer_serviced <- (1 - PN)*100 a <- cbind( Lq, Wq, Ls, Ws, customer_serviced ) return(a) } mu <- 4 res <- Map( myfun, list( 21 ,21, 22, 22 ,22, 22 ,21 ), list( 16, 17, 17, 17, 18, 17 ,17 ), mu) head( res[[1]]) # [,1] [,2] [,3] [,4] # [1,] 1 42.184874 19.04762 1490400 # [2,] 2 38.241748 38.09526 1391040 # [3,] 3 33.339271 57.13862 1291680 # [4,] 4 26.014138 75.70348 1192320 # [5,] 5 16.339462 89.88989 1092960 # [6,] 6 9.121053 96.32498 993600

更多推荐

本文发布于:2023-07-27 08:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1288004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   矩阵   如何在   matrix   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!