R中矩阵对角元素的去除
如何使用R从矩阵L中删除对角线元素(diagL)?我尝试使用以下方法:R中矩阵对角元素的去除,r,matrix,diagonal,R,Matrix,Diagonal,如何使用R从矩阵L中删除对角线元素(diagL)?我尝试使用以下方法: subset(L, select=-diag(L)) or subset(L, select=-c(diag(L))) 但是我得到了0个数字…记住对角线的X和Y索引是相同的。下面是一个快速的程序,用于将C中的对角线归零: #include <stdio.h> static void printMat(char mat[4][4], char *comment) { printf("%s:\n", com
subset(L, select=-diag(L)) or
subset(L, select=-c(diag(L)))
但是我得到了0个数字…记住对角线的X和Y索引是相同的。下面是一个快速的程序,用于将C中的对角线归零:
#include <stdio.h>
static void printMat(char mat[4][4], char *comment)
{
printf("%s:\n", comment);
for(int jj=0; jj<4; jj++) {
for(int ii=0; ii<4; ii++) {
printf("%2d ",mat[jj][ii]);
}
printf("\n");
}
}
main()
{
static char matrix[4][4]= {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}
};
printMat(matrix,"Before");
for(int ii=0; ii<4; ii++) {
matrix[ii][ii]=0;
}
printMat(matrix,"After");
}
要删除而不是只清除对角线更复杂
这应该可以做到:(请记住,零字节的memcpy可以寻址不存在的元素。)
当然,如果你想用另一种语言写东西,问一下会有帮助。R编程语言?我更喜欢C,它更容易拼写 一种方法是以我喜欢的方式创建一个包含数字的矩阵:
a<-t(matrix(1:16,nrow=4,ncol=4))
删除对角线上的值:
diag(a)=NA
其结果是:
Before:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After:
0 2 3 4
5 0 7 8
9 10 0 12
13 14 15 0
Before:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After:
2 3 4
5 7 8
9 10 12
13 14 15
[,1] [,2] [,3] [,4]
[1,] NA 2 3 4
[2,] 5 NA 7 8
[3,] 9 10 NA 12
[4,] 13 14 15 NA
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 5 7 8
[3,] 9 10 12
[4,] 13 14 15
要真正删除这些值,而不仅仅是让它们消失,我们需要重铸:
a<-t(matrix(t(a)[which(!is.na(a))],nrow=3,ncol=4))
这和我们在上面的C中得到的一样
这有点迂回,但我认为这是一个正确的答案。我很想看到比我更了解R的人改进解决方案
关于作业的一点解释:
a<-t(matrix(t(a)[which(!is.na(a))],nrow=3,ncol=4))
a以下是一些人工数据用于说明:
x <- matrix(1:16, 4, 4)
n <- nrow(x)
x
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
在“删除矩阵的对角线”之后,您可以将下三角矩阵向上移动,以获得具有n-1
行和n
列的矩阵
matrix(x[-seq(1,n^2,n+1)], n-1, n)
[,1] [,2] [,3] [,4]
[1,] 2 5 9 13
[2,] 3 7 10 14
[3,] 4 8 12 15
或者,这可能是您想要的,您可以将下三角矩阵向右移动,通过在移除对角线索引之前转置x
,然后将其转置回来,得到一个包含n
行和n-1
列的矩阵
t(matrix(t(x)[-seq(1,n^2,n+1)], n-1, n))
[,1] [,2] [,3]
[1,] 5 9 13
[2,] 2 10 14
[3,] 3 7 15
[4,] 4 8 12
仍然使用basic R,可以使用upper.tri()
和lower.tri
的组合,在一行中查找您要查找的内容。为了方便起见,我创建了一个单行函数。代码如下
a <- matrix(rnorm(100), nrow = 4, ncol = 4)
select_all_but_diag <- function(x) matrix(x[lower.tri(x, diag = F) | upper.tri(x, diag = F)], nrow = nrow(x) - 1, ncol = ncol(x))
select_all_but_diag(a)
这是选择除诊断(a)之外的所有诊断矩阵输出矩阵:
[,1] [,2] [,3] [,4]
[1,] 0.7 2.5 -0.5 2.8
[2,] 0.9 0.8 -1.4 -0.7
[3,] -0.8 -0.3 -0.9 0.5
编辑行主键
相反,如果希望折叠为行主,可以使用此扩展版本的函数,该函数允许您折叠矩阵,减少列数而不是行数
select_all_but_diag <- function(x, collapse_by = "row") {
if(collapse_by == "row") matrix(x[lower.tri(x, diag = F) | upper.tri(x, diag = F)], nrow = nrow(x) - 1, ncol = ncol(x))
else if(collapse_by == "col") t(matrix(t(x)[lower.tri(x, diag = F) | upper.tri(x, diag = F)], nrow = nrow(x) - 1, ncol = ncol(x)))
else stop("collapse_by accepts only 'row' or 'col'.")
}
a
select_all_but_diag(a, collapse_by = "col")
你用什么计算机语言?哪种语言?你说的移除是什么意思?设置为零?@us2012我的意思是从矩阵中删除它们。例如,这个答案实际上不起作用
a <- matrix(rnorm(100), nrow = 4, ncol = 4)
select_all_but_diag <- function(x) matrix(x[lower.tri(x, diag = F) | upper.tri(x, diag = F)], nrow = nrow(x) - 1, ncol = ncol(x))
select_all_but_diag(a)
[,1] [,2] [,3] [,4]
[1,] 0.3 2.5 -0.5 2.8
[2,] 0.7 1.1 -1.4 -0.7
[3,] 0.9 0.8 1.6 0.5
[4,] -0.8 -0.3 -0.9 1.6
[,1] [,2] [,3] [,4]
[1,] 0.7 2.5 -0.5 2.8
[2,] 0.9 0.8 -1.4 -0.7
[3,] -0.8 -0.3 -0.9 0.5
select_all_but_diag <- function(x, collapse_by = "row") {
if(collapse_by == "row") matrix(x[lower.tri(x, diag = F) | upper.tri(x, diag = F)], nrow = nrow(x) - 1, ncol = ncol(x))
else if(collapse_by == "col") t(matrix(t(x)[lower.tri(x, diag = F) | upper.tri(x, diag = F)], nrow = nrow(x) - 1, ncol = ncol(x)))
else stop("collapse_by accepts only 'row' or 'col'.")
}
a
select_all_but_diag(a, collapse_by = "col")
[,1] [,2] [,3]
[1,] 2.5 -0.5 2.8
[2,] 0.7 -1.4 -0.7
[3,] 0.9 0.8 0.5
[4,] -0.8 -0.3 -0.9