楼主: qoiqpwqr
8142 2

[实际应用] 怎样输入这样的矩阵比较方便? [推广有奖]

已卖:100份资源

院士

49%

还不是VIP/贵宾

-

威望
1
论坛币
132048 个
通用积分
9219.7014
学术水平
925 点
热心指数
1073 点
信用等级
703 点
经验
130943 点
帖子
3353
精华
1
在线时间
3498 小时
注册时间
2009-7-18
最后登录
2025-5-18

初级热心勋章 初级信用勋章 初级学术勋章 中级热心勋章 高级热心勋章

楼主
qoiqpwqr 发表于 2011-11-10 21:34:20 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
在实验设计时经常碰到这样的矩阵。该矩阵是把一个单位矩阵的每一行重复一定的次数。      [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    0    0
[2,]    1    0    0    0    0
[3,]    1    0    0    0    0
[4,]    0    1    0    0    0
[5,]    0    1    0    0    0
[6,]    0    1    0    0    0
[7,]    0    1    0    0    0
[8,]    0    1    0    0    0
[9,]    0    0    1    0    0
[10,]    0    0    1    0    0
[11,]    0    0    0    1    0
[12,]    0    0    0    1    0
[13,]    0    0    0    1    0
[14,]    0    0    0    1    0
[15,]    0    0    0    0    1


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我的做法是先写了个函数,输入是一个给定的矩阵和每一行要重复的次数,输出则是想要的矩阵。
repMat <- function(x, each = NULL) {
    if (is.null(each)) return(x)
    n <- nrow(x)
    if (length(each) != n) stop("Number of rows in x should be equal to the length of each!")
    res <- NULL
    for (i in 1:n) {
        currentline <- matrix(rep(x[i, ], each), nrow = each, byrow = TRUE)
        res <- rbind(res, currentline)
    }
    return(res)
}

然后
x <- diag(5)
repMat(x, c(3,5,2,4,1))
就可以了。


不知道还有没有更方便的方法。
二维码

扫码加我 拉你入群

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

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

关键词:实验设计 Mat PMA

沙发
李婷婷123 发表于 2011-11-10 21:48:13
自问自答

藤椅
zhangyangsmith 发表于 2011-11-11 06:42:34
Here is just another way of looking at the question. Can not really say it's easier...
  1. > # Dim of Indentity Matrix
  2. > nI <- 5
  3. > # Times of replication
  4. > nRep <- c(3, 5, 2, 4, 1)
  5. > # Target Matrix
  6. > myMtr <- matrix(0, ncol = nI, nrow = sum(nRep))
  7. > # Change corresponding elements to 1
  8. > myMtr[cbind(1:sum(nRep), rep(1:nI, times = nRep))] <- 1
  9. > # Results
  10. > myMtr
  11.       [,1] [,2] [,3] [,4] [,5]
  12. [1,]    1    0    0    0    0
  13. [2,]    1    0    0    0    0
  14. [3,]    1    0    0    0    0
  15. [4,]    0    1    0    0    0
  16. [5,]    0    1    0    0    0
  17. [6,]    0    1    0    0    0
  18. [7,]    0    1    0    0    0
  19. [8,]    0    1    0    0    0
  20. [9,]    0    0    1    0    0
  21. [10,]    0    0    1    0    0
  22. [11,]    0    0    0    1    0
  23. [12,]    0    0    0    1    0
  24. [13,]    0    0    0    1    0
  25. [14,]    0    0    0    1    0
  26. [15,]    0    0    0    0    1
复制代码
Certainly you can write a function with this method.

板凳
zhangyangsmith 发表于 2011-11-11 06:44:15
Here is just another way of looking at the question. Can not really say it's easier...
  1. > # Dim of Indentity Matrix
  2. > nI <- 5
  3. > # Times of replication
  4. > nRep <- c(3, 5, 2, 4, 1)
  5. > # Target Matrix
  6. > myMtr <- matrix(0, ncol = nI, nrow = sum(nRep))
  7. > # Change corresponding elements to 1
  8. > myMtr[cbind(1:sum(nRep), rep(1:nI, times = nRep))] <- 1
  9. > # Results
  10. > myMtr
  11.       [,1] [,2] [,3] [,4] [,5]
  12. [1,]    1    0    0    0    0
  13. [2,]    1    0    0    0    0
  14. [3,]    1    0    0    0    0
  15. [4,]    0    1    0    0    0
  16. [5,]    0    1    0    0    0
  17. [6,]    0    1    0    0    0
  18. [7,]    0    1    0    0    0
  19. [8,]    0    1    0    0    0
  20. [9,]    0    0    1    0    0
  21. [10,]    0    0    1    0    0
  22. [11,]    0    0    0    1    0
  23. [12,]    0    0    0    1    0
  24. [13,]    0    0    0    1    0
  25. [14,]    0    0    0    1    0
  26. [15,]    0    0    0    0    1
复制代码
Certainly you can write a function with this method.

报纸
zhangyangsmith 发表于 2011-11-11 06:49:50
Here is just another way of looking at the question. Can not really say it's easier...
  1. > # Dim of Indentity Matrix
  2. > nI <- 5
  3. > # Times of replication
  4. > nRep <- c(3, 5, 2, 4, 1)
  5. > # Target Matrix
  6. > myMtr <- matrix(0, ncol = nI, nrow = sum(nRep))
  7. > # Change corresponding elements to 1
  8. > myMtr[cbind(1:sum(nRep), rep(1:nI, times = nRep))] <- 1
  9. > # Results
  10. > myMtr
  11.       [,1] [,2] [,3] [,4] [,5]
  12. [1,]    1    0    0    0    0
  13. [2,]    1    0    0    0    0
  14. [3,]    1    0    0    0    0
  15. [4,]    0    1    0    0    0
  16. [5,]    0    1    0    0    0
  17. [6,]    0    1    0    0    0
  18. [7,]    0    1    0    0    0
  19. [8,]    0    1    0    0    0
  20. [9,]    0    0    1    0    0
  21. [10,]    0    0    1    0    0
  22. [11,]    0    0    0    1    0
  23. [12,]    0    0    0    1    0
  24. [13,]    0    0    0    1    0
  25. [14,]    0    0    0    1    0
  26. [15,]    0    0    0    0    1
复制代码
Certainly you can write a function with this method.

地板
shenbaiseshatan 在职认证  发表于 2011-11-12 00:15:40
偷懒法
  1. n<-5
  2. x<-diag(n)
  3. r<-c(3, 5, 2, 4, 1)
  4. (res<-matrix(unlist(lapply(1:5,function(i){rep(x[i,],r[i])})),ncol=5,byrow=T))
复制代码
胜人者有力,自胜者强!

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

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