有效地将矩阵拆分为中心(Efficiently split matrix down its centre)

编程入门 行业动态 更新时间:2024-10-25 12:26:59
有效地将矩阵拆分为中心(Efficiently split matrix down its centre)

给出以下矩阵:

set.seed(1) x <- matrix(rnorm(15),5,3) [,1] [,2] [,3] [1,] -0.6264538 -0.8204684 1.5117812 [2,] 0.1836433 0.4874291 0.3898432 [3,] -0.8356286 0.7383247 -0.6212406 [4,] 1.5952808 0.5757814 -2.2146999 [5,] 0.3295078 -0.3053884 1.1249309

我想提供一个行数与x相同的向量,整数值介于1和x的列数之间,例如

split_vector = c(1, 2, 3, 1, 2)

并应用“将矩阵x分成三个矩阵”的操作,每个矩阵的大小与x相同。 第一个包含split_vector相应索引左侧每行中的所有列,第二个包含split_vector索引给出的条目,第三个包含所有其他条目。 例如,分别将这些矩阵表示为M1, M2, M3 ,然后:

M2 <- matrix(rep(F, prod(dim(x))), ncol = ncol(x)) M2[cbind(seq_len(nrow(x)), split_vector)] <- T M2[M2] <- x[M2] # very naughty coercion here > M2 [,1] [,2] [,3] [1,] -0.6264538 0.0000000 0.0000000 [2,] 0.0000000 0.4874291 0.0000000 [3,] 0.0000000 0.0000000 -0.6212406 [4,] 1.5952808 0.0000000 0.0000000 [5,] 0.0000000 -0.3053884 0.0000000

Given the following matrix:

set.seed(1) x <- matrix(rnorm(15),5,3) [,1] [,2] [,3] [1,] -0.6264538 -0.8204684 1.5117812 [2,] 0.1836433 0.4874291 0.3898432 [3,] -0.8356286 0.7383247 -0.6212406 [4,] 1.5952808 0.5757814 -2.2146999 [5,] 0.3295078 -0.3053884 1.1249309

I would like to supply a vector with the same number of rows as x with integer values between 1 and the number of columns of x, e.g.

split_vector = c(1, 2, 3, 1, 2)

and apply the operation that 'splits the matrix x into three matrices', each of the same size as x. The first contains all columns in each row to the left of the corresponding index in split_vector, the second containing entries given by the indices in split_vector, and the third containing all other entries. For example, denote these matrices as M1, M2, M3 respectively, then:

M2 <- matrix(rep(F, prod(dim(x))), ncol = ncol(x)) M2[cbind(seq_len(nrow(x)), split_vector)] <- T M2[M2] <- x[M2] # very naughty coercion here > M2 [,1] [,2] [,3] [1,] -0.6264538 0.0000000 0.0000000 [2,] 0.0000000 0.4874291 0.0000000 [3,] 0.0000000 0.0000000 -0.6212406 [4,] 1.5952808 0.0000000 0.0000000 [5,] 0.0000000 -0.3053884 0.0000000

最满意答案

创建输出矩阵

out1 <- out2 <- out3 <- matrix(0, nrow(x), ncol(x))

创建满足每个条件的指标,并用于从x添加相关值

id1 <- col(x) < v out1[id1] <- x[id1] id2 <- col(x) == v out2[id2] <- x[id2] id3 <- !(id2 + id1) out3[id3] <- x[id3]

产量

> out1 [,1] [,2] [,3] [1,] 0.0000000 0.0000000 0 [2,] 0.1836433 0.0000000 0 [3,] -0.8356286 0.7383247 0 [4,] 0.0000000 0.0000000 0 [5,] 0.3295078 0.0000000 0 > out2 [,1] [,2] [,3] [1,] -0.6264538 0.0000000 0.0000000 [2,] 0.0000000 0.4874291 0.0000000 [3,] 0.0000000 0.0000000 -0.6212406 [4,] 1.5952808 0.0000000 0.0000000 [5,] 0.0000000 -0.3053884 0.0000000 > out3 [,1] [,2] [,3] [1,] 0 -0.8204684 1.5117812 [2,] 0 0.0000000 0.3898432 [3,] 0 0.0000000 0.0000000 [4,] 0 0.5757814 -2.2146999 [5,] 0 0.0000000 1.1249309

Create output matrix

out1 <- out2 <- out3 <- matrix(0, nrow(x), ncol(x))

Create indicators that meet each condition and use to add relevant values from x

id1 <- col(x) < v out1[id1] <- x[id1] id2 <- col(x) == v out2[id2] <- x[id2] id3 <- !(id2 + id1) out3[id3] <- x[id3]

Output

> out1 [,1] [,2] [,3] [1,] 0.0000000 0.0000000 0 [2,] 0.1836433 0.0000000 0 [3,] -0.8356286 0.7383247 0 [4,] 0.0000000 0.0000000 0 [5,] 0.3295078 0.0000000 0 > out2 [,1] [,2] [,3] [1,] -0.6264538 0.0000000 0.0000000 [2,] 0.0000000 0.4874291 0.0000000 [3,] 0.0000000 0.0000000 -0.6212406 [4,] 1.5952808 0.0000000 0.0000000 [5,] 0.0000000 -0.3053884 0.0000000 > out3 [,1] [,2] [,3] [1,] 0 -0.8204684 1.5117812 [2,] 0 0.0000000 0.3898432 [3,] 0 0.0000000 0.0000000 [4,] 0 0.5757814 -2.2146999 [5,] 0 0.0000000 1.1249309

更多推荐

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

发布评论

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

>www.elefans.com

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