R中“对角”矩阵的平方

时间:2018-07-17 22:39:32

标签: r matrix

我有一组向量,并希望将它们彼此堆叠以创建矩阵的对角线项。

从c1和c2创建example_out矩阵的简单方法是什么?

BrowserRouter

5 个答案:

答案 0 :(得分:6)

0中创建矩阵,然后用c1填充主对角线,并用c2填充子对角线。

example_out <- matrix(rep(0, 20), 5, 4)
diag(example_out) <- c1
diag(example_out[-1, ]) <- c2

屈服

> example_out
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    5    2    0    0
[3,]    0    6    3    0
[4,]    0    0    7    4
[5,]    0    0    0    8

数据

c1 <- seq(1, 4)
c2 <- seq(5, 8)

答案 1 :(得分:3)

这是一种替代方法,在转换为replace之前numeric向量中的条目matrix

matrix(unlist(sapply(seq_along(c1), function(i)
    replace(rep(0, length(c1) + 1), i:(i+1), c(c1[i], c2[i])))),
    ncol = length(c1))
#    [,1] [,2] [,3] [,4]
#[1,]    1    0    0    0
#[2,]    5    2    0    0
#[3,]    0    6    3    0
#[4,]    0    0    7    4
#[5,]    0    0    0    8

更新

我很想知道不同方法在性能/运行时间方面如何进行比较。这是简短的microbenchmark分析,其中使用了两个较大的vectorc1c2

set.seed(2017)
c1 <- sample(1000)
c2 <- sample(1000)

library(microbenchmark)
library(Matrix)

res <- microbenchmark(
    method_jaySF = {
        example_out <- matrix(0, length(c1) + 1, length(c2))
        diag(example_out) <- c1
        diag(example_out[-1, ]) <- c2
    },
    method_Roland = {
        bandSparse(length(c1) + 1, length(c2), 0:-1, list(c1, c2))
    },
    method_Onyambu = {
        a = matrix(0,length(c1)+1,length(c2))
        a[row(a)==col(a)]=c1
        a[row(a)==col(a)+1]=c2
    },
    method_Gregor = {
        rbind(diag(c1), 0) + rbind(0, diag(c2))
    },
    method_Maurits = {
        matrix(unlist(sapply(seq_along(c1), function(i)
    replace(rep(0, length(c1) + 1), i:(i+1), c(c1[i], c2[i])))),
    ncol = length(c1))
    }
)
res;
#Unit: microseconds
#           expr       min       lq      mean    median         uq        max
#   method_jaySF 31894.439 37850.81  58452.41 40560.992  46224.579 208862.631
#  method_Roland   940.535  1342.32   1675.29  1457.928   1869.621   8228.287
# method_Onyambu 55033.797 66083.67 124364.44 73143.798 195886.534 274383.132
#  method_Gregor 37784.977 44049.87  69918.85 47539.793  53122.162 243774.715
# method_Maurits 14961.924 21378.77  42834.89 23536.966  27270.953 186088.146

autoplot(res)

enter image description here

答案 2 :(得分:3)

另一种简单的方法,用0行扩充两个对角矩阵并将它们相加:

rbind(diag(c1), 0) + rbind(0, diag(c2))
#      [,1] [,2] [,3] [,4]
# [1,]    1    0    0    0
# [2,]    5    2    0    0
# [3,]    0    6    3    0
# [4,]    0    0    7    4
# [5,]    0    0    0    8

答案 3 :(得分:2)

您应该创建一个稀疏矩阵。使用Matrix软件包:

t_zeros = K.cast(t_zeros, K.floatx())
t_ones = K.cast(t_ones, K.floatx())

答案 4 :(得分:1)

 a = matrix(0,length(c1)+1,length(c2))
 a[row(a)==col(a)]=c1
 a[row(a)==col(a)+1]=c2
 a
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    5    2    0    0
[3,]    0    6    3    0
[4,]    0    0    7    4
[5,]    0    0    0    8