I guess, I have a two leveled question referring to diag
in R and matlab.
我想,在R和matlab中,我有两个关于diag的问题。
1) I was wondering if there was a way already developed to access different diagonals of matrices in R similar to the way it is done in Matlab (see http://www.mathworks.com/help/techdoc/ref/diag.html).
1)我想知道是否有一种方法可以在R中访问不同的矩阵对角线,类似于在Matlab中完成的方法(参见http://www.mathworks.com/help/techdoc/ref/diag.html)。
2) If there is not already a current function how can my code be improved such that it is similar to the R diag
where
2)如果没有当前的函数,如何改进我的代码,使其与R diag类似。
diag(x = 1, nrow, ncol) # returns the values of the diagonal
diag(x) <- value # inserts values on the diagonal
Presently my code returns the elements on the diagonal given k but how can it be written so that if it is specified like the second way (above) that it allows me to insert the values on the diagonal. Presently to do this, I use diag.ind
to give me the indices and then using those indices to insert the values on the k diagonal.
现在,我的代码返回了对角线上的元素,但是如何写,如果它被指定为第二种方式(上面),它允许我在对角线上插入值。现在要做这个,我用对角线来给我指数,然后用这些指标来插入k对角线上的值。
Here is the code:
这是代码:
'diag.ind'<-function(x,k=0){
if(k=='') k=0
x<-as.matrix(x)
if(dim(x)[2]==dim(x)[1]){
stp_pt_r<-dim(x)[1]
stp_pt_c<-dim(x)[2]
}
if(ncol(x)> dim(x)[1]){
stp_pt_r<-dim(x)[1]
stp_pt_c<-stp_pt_r + 1
}
if(ncol(x)< dim(x)[1]){
stp_pt_c<-dim(x)[2]
stp_pt_r<-stp_pt_c+1
}
if(k==0){
r<-as.matrix(seq(1,stp_pt_r,by=1))
c<-as.matrix(seq(1,stp_pt_c,by=1))
ind.r<- cbind(r,c)
}
if(k>0){
r<-t(as.matrix(seq(1,stp_pt_r,by=1)))
c<-t(as.matrix(seq((1+k),stp_pt_c,by=1)))
ind<-t(rbind.fill.matrix(r,c))
ind.r<-ind[!is.na(ind[,2]),]
}
if(k<0){
k<-abs(k)
r<-t(as.matrix(seq((1+k),stp_pt_r,by=1)))
c<-t(as.matrix(seq(1,stp_pt_c,by=1)))
ind<-t(rbind.fill.matrix(r,c))
ind.r<-ind[!is.na(ind[,1]),]
}
diag.x<-x[ind.r]
output<-list(diag.x=diag.x, diag.ind=ind.r)
return(output)
}
This is kind of clunky and I feel like I must be reinventing the wheel. Thanks in advance for any insight!
这有点笨拙,我觉得我必须重新发明*。感谢您的任何洞察力!
2 个解决方案
#1
3
After your reply to Andrie this may satisfy:
在你对Andrie的答复后,这可能满足:
exdiag <- function(mat, off) {mat[row(mat)+off == col(mat)]}
x <- matrix(1:16, ncol=4)
exdiag(x,1)
#[1] 5 10 15
I was thinking you wanted a function that can assign or return one of a diagonal or a sub- or super- diagonal matrix, This is the constructor function:
我认为你想要一个函数可以分配或返回一个对角线或超对角矩阵,这是构造函数
subdiag <- function(vec, size, offset=0){
M <- matrix(0, size, size)
M[row(M)-offset == col(M)] <- vec
return(M)}
> subdiag(1, 5, 1)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 1 0 0 0 0
[3,] 0 1 0 0 0
[4,] 0 0 1 0 0
[5,] 0 0 0 1 0
Called with only two arguments you would get a diagonal matrix. You can construct super-diagonal matrices with negative offsets. If this is what you wanted for the constructor, then it should not be too hard to construct a similar subdiag<-
function to go along with it.
只需要两个参数就可以得到一个对角矩阵。你可以构造带有负偏移量的超对角矩阵。如果这是您想要的构造函数,那么构造一个类似的subdiag<-函数也不太困难。
#2
0
In MATLAB, to assign the values x
to the diagonal of A
:
在MATLAB中,将值x分配到A的对角线上:
n = size(A,1);
A(1:n+1:end) = x
Look up linear indexing.
线性索引查找。
Although, that might not be what you asked.
虽然,这可能不是你想要的。
#1
3
After your reply to Andrie this may satisfy:
在你对Andrie的答复后,这可能满足:
exdiag <- function(mat, off) {mat[row(mat)+off == col(mat)]}
x <- matrix(1:16, ncol=4)
exdiag(x,1)
#[1] 5 10 15
I was thinking you wanted a function that can assign or return one of a diagonal or a sub- or super- diagonal matrix, This is the constructor function:
我认为你想要一个函数可以分配或返回一个对角线或超对角矩阵,这是构造函数
subdiag <- function(vec, size, offset=0){
M <- matrix(0, size, size)
M[row(M)-offset == col(M)] <- vec
return(M)}
> subdiag(1, 5, 1)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 1 0 0 0 0
[3,] 0 1 0 0 0
[4,] 0 0 1 0 0
[5,] 0 0 0 1 0
Called with only two arguments you would get a diagonal matrix. You can construct super-diagonal matrices with negative offsets. If this is what you wanted for the constructor, then it should not be too hard to construct a similar subdiag<-
function to go along with it.
只需要两个参数就可以得到一个对角矩阵。你可以构造带有负偏移量的超对角矩阵。如果这是您想要的构造函数,那么构造一个类似的subdiag<-函数也不太困难。
#2
0
In MATLAB, to assign the values x
to the diagonal of A
:
在MATLAB中,将值x分配到A的对角线上:
n = size(A,1);
A(1:n+1:end) = x
Look up linear indexing.
线性索引查找。
Although, that might not be what you asked.
虽然,这可能不是你想要的。