1
我需要寫兩個函數toBand()
和replaceBand()
:r功能來創建帶矩陣
test3 <- toBand(test1,3)
test4 <- replaceBand(test1, toBand(test2,3))
得到下面的輸出。
第一個返回對角線和對角線元素的矩陣,使用X的下三角形。第二個從所提供的頻帶矩陣中的數據中替換X中的對應元素。該函數然後返回修改的X.
是否有任何可用於執行此操作的軟件包?有關如何做到這一點的任何建議?
謝謝
> test1
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a11" "a21" "a31" "a41" "a51" "a61"
[2,] "a21" "a22" "a32" "a42" "a52" "a62"
[3,] "a31" "a32" "a33" "a43" "a53" "a63"
[4,] "a41" "a42" "a43" "a44" "a54" "a64"
[5,] "a51" "a52" "a53" "a54" "a55" "a65"
[6,] "a61" "a62" "a63" "a64" "a65" "a66"
> test2
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "*a11*" "*a21*" "*a31*" "*a41*" "*a51*" "*a61*"
[2,] "*a21*" "*a22*" "*a32*" "*a42*" "*a52*" "*a62*"
[3,] "*a31*" "*a32*" "*a33*" "*a43*" "*a53*" "*a63*"
[4,] "*a41*" "*a42*" "*a43*" "*a44*" "*a54*" "*a64*"
[5,] "*a51*" "*a52*" "*a53*" "*a54*" "*a55*" "*a65*"
[6,] "*a61*" "*a62*" "*a63*" "*a64*" "*a65*" "*a66*"
> test3
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a11" "a22" "a33" "a44" "a55" "a66"
[2,] "a21" "a32" "a43" "a54" "a65" NA
[3,] "a31" "a42" "a53" "a64" NA NA
[4,] "a41" "a52" "a63" NA NA NA
> test4
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "*a11*" "*a21*" "*a31*" "*a41*" "a51" "a61"
[2,] "*a21*" "*a22*" "*a32*" "*a42*" "*a52*" "a62"
[3,] "*a31*" "*a32*" "*a33*" "*a43*" "*a53*" "*a63*"
[4,] "*a41*" "*a42*" "*a43*" "*a44*" "*a54*" "*a64*"
[5,] "a51" "*a52*" "*a53*" "*a54*" "*a55*" "*a65*"
[6,] "a61" "a62" "*a63*" "*a64*" "*a65*" "*a66*"
何亞倫,謝謝您的回覆。我還有一個天真的問題。如何爲需要矩陣和k值的S4「Band」類編寫initialize()方法?它應該被定義爲具有形式參數(x,k)的函數,並且應該將X的下三角形元素放入對象中。 – user635034 2011-03-04 03:40:36
@ user635034:我只使用S3方法。如果通常的S4文檔不清楚,可以試試Hadley的wiki:https://github.com/hadley/devtools/wiki/S4 – Aaron 2011-03-04 05:12:16