โค้ดด้านล่างแสดงวิธีที่ฉันใช้วิธีการของ Joshua Ulrich เพื่อสร้างเมทริกซ์ที่ซับซ้อนขึ้นเล็กน้อย หวังว่าคำตอบนี้จะมีประโยชน์คือการแสดงความยืดหยุ่นที่เป็นไปได้ในการสร้างวัตถุ ถ้าไม่ฉันสามารถลบคำตอบของฉันได้
ฉันสงสัยว่าวิธีการนี้สามารถปรับเปลี่ยนได้อย่างง่ายดายเพื่อสร้างเมทริกซ์ที่มีขนาดแตกต่างกัน เช่น โดยการตั้งค่า nrow
หรือ ncol
เท่ากับตัวแปร และใช้ rep(q, z)
กับตัวแปรบางตัว z
เพื่อทำซ้ำองค์ประกอบในเวกเตอร์ภายในคำสั่ง matrix
หรือ rbind
:
p1.c1 <- 0.10
p2.c1 <- 0.20
p3.c1 <- 0.30
p4.c1 <- 0.40
s1.c1 <- matrix(c(p1.c1, p1.c1, (1 - p1.c1),
p1.c1, p1.c1, (1 - p1.c1),
0, 0, 1), nrow=3, ncol=3, byrow = TRUE)
s2.c1 <- matrix(c(p2.c1, p2.c1, (1 - p2.c1),
p2.c1, p2.c1, (1 - p2.c1),
0, 0, 1), nrow=3, ncol=3, byrow = TRUE)
s3.c1 <- matrix(c(p3.c1, p3.c1, (1 - p3.c1),
p3.c1, p3.c1, (1 - p3.c1),
0, 0, 1), nrow=3, ncol=3, byrow = TRUE)
s4.c1 <- matrix(c(p4.c1, p4.c1, (1 - p4.c1),
p4.c1, p4.c1, (1 - p4.c1),
0, 0, 1), nrow=3, ncol=3, byrow = TRUE)
n <- 5
p.c1 <- c(p1.c1, p2.c1, p3.c1, p4.c1)
for (i in 1: (n - 1)) {
assign(paste('xs', i, '.c1', sep=""), matrix(c(p.c1[i], p.c1[i], (1-p.c1[i]),
p.c1[i], p.c1[i], (1-p.c1[i]),
0, 0, 1 ), nrow=3, ncol=3, byrow = TRUE))
}
identical(xs1.c1, s1.c1)
identical(xs2.c1, s2.c1)
identical(xs3.c1, s3.c1)
identical(xs4.c1, s4.c1)
for (i in 1: (n - 1)) {
assign(paste('ys', i, '.c1', sep=""), rbind(c(p.c1[i], p.c1[i], (1-p.c1[i])),
c(p.c1[i], p.c1[i], (1-p.c1[i])),
c(0, 0, 1)))
}
identical(ys1.c1, s1.c1)
identical(ys2.c1, s2.c1)
identical(ys3.c1, s3.c1)
identical(ys4.c1, s4.c1)
person
Mark Miller
schedule
01.03.2018