An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090311/b88bf19f/attachment-0002.pl>
Matrix Construction; Subdiagonal
8 messages · Sundar Dorai-Raj, Bill Venables, Stu Field +1 more
Does this help? A <- matrix(0, 6, 6) vec <- 1:5 A[row(A) == col(A) + 1] <- vec --sundar
On Wed, Mar 11, 2009 at 4:42 PM, Stu Field <sgf at colostate.edu> wrote:
I'm trying to enter a vector into the subdiagonal of a matrix but cannot find a command in R which corresponds to the MatLab version of diag(vec, k), where vec = the vector of interest, and k = the diagonal (k=0 for the diagonal; k=-1 for the subdiagonal; k=1 for superdiagonal, etc.) Is there an equivalent command in R? I'm looking for something like this: vec = seq(1, 5, 1) ? ? ? ?# vector of interest A = xyz(vec,-1) ? ? ? ? ? # creates a 6x6 matrix with vec on the subdiagonal where xyz is some function similar to diag, but with differing arguments. I can't believe there is not a simple way to do this... Thanks for your help, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Stu Field, PhD Postdoctoral Fellow Department of Biology Colorado State University 1878 Campus Delivery Fort Collins, CO 80523-1878 Office: E208 Anatomy/Zoology Phone: (970) 491-5744 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090311/960d752e/attachment-0002.pl>
You can always write your own function:
myDiag <- function(x, vec, k) {
x[row(x) == col(x) - k] <- vec
x
}
myDiag(A, vec, -1)
Of course, you should probably do some input checking too.
--sundar
On Wed, Mar 11, 2009 at 4:57 PM, Stu Field <sgf at colostate.edu> wrote:
Sure, that'll work fine, thanks. But I guess I was looking for something more similar to MatLab, I'm really surprised R doesn't have a preset command for this (?) Thanks again, Stu On 11 ? Mar ? 2009, at 5:49 PM, Sundar Dorai-Raj wrote: Does this help? A <- matrix(0, 6, 6) vec <- 1:5 A[row(A) == col(A) + 1] <- vec --sundar On Wed, Mar 11, 2009 at 4:42 PM, Stu Field <sgf at colostate.edu> wrote: I'm trying to enter a vector into the subdiagonal of a matrix but cannot find a command in R which corresponds to the MatLab version of diag(vec, k), where vec = the vector of interest, and k = the diagonal (k=0 for the diagonal; k=-1 for the subdiagonal; k=1 for superdiagonal, etc.) Is there an equivalent command in R? I'm looking for something like this: vec = seq(1, 5, 1) ? ? ? ?# vector of interest A = xyz(vec,-1) ? ? ? ? ? # creates a 6x6 matrix with vec on the subdiagonal where xyz is some function similar to diag, but with differing arguments. I can't believe there is not a simple way to do this... Thanks for your help, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Stu Field, PhD Postdoctoral Fellow Department of Biology Colorado State University 1878 Campus Delivery Fort Collins, CO 80523-1878 Office: E208 Anatomy/Zoology Phone: (970) 491-5744 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Stu Field, PhD Postdoctoral Fellow Department of Biology Colorado State University 1878 Campus Delivery Fort Collins, CO 80523-1878 Office: E208 Anatomy/Zoology Phone: (970) 491-5744 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Why should there be a simple way? R is not primarily a matrix language.
Perhaps this will help:
xyz <- function (v, k) {
n <- length(v) + abs(k)
x <- matrix(0, n, n)
if (k == 0)
diag(x) <- v
else if (k < 0)
{ ## sub-diagonal
j <- 1:(n+k)
i <- (1 - k):n
x[cbind(i, j)] <- v
} else
{ ## super-diagonal
i <- 1:(n-k)
k <- (1 + k):n
x[cbind(i, j)] <- v
}
x
}
modify, extend, simplify, ... as you wish.
Bill Venables.
Bill Venables
http://www.cmis.csiro.au/bill.venables/
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Stu Field
Sent: Thursday, 12 March 2009 9:43 AM
To: r-help at r-project.org
Subject: [R] Matrix Construction; Subdiagonal
I'm trying to enter a vector into the subdiagonal of a matrix but
cannot find a command in R which corresponds to the MatLab version of
diag(vec, k), where vec = the vector of interest, and k = the diagonal
(k=0 for the diagonal; k=-1 for the subdiagonal; k=1 for
superdiagonal, etc.)
Is there an equivalent command in R?
I'm looking for something like this:
vec = seq(1, 5, 1) # vector of interest
A = xyz(vec,-1) # creates a 6x6 matrix with vec on the
subdiagonal
where xyz is some function similar to diag, but with differing
arguments.
I can't believe there is not a simple way to do this...
Thanks for your help,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stu Field, PhD
Postdoctoral Fellow
Department of Biology
Colorado State University
1878 Campus Delivery
Fort Collins, CO 80523-1878
Office: E208 Anatomy/Zoology
Phone: (970) 491-5744
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
This is horrible on memory if you are dealing with large matrices.
Here is a slightly more slick version of the function I have already posted:
xyz <- function (v, k) {
n <- length(v) + abs(k)
x <- matrix(0, n, n)
i <- (1 - min(0, k)):(n - max(0,k))
j <- (1 + max(0, k)):(n + min(0,k))
x[cbind(i,j)] <- v
x
}
The point of this is that it does not hold three copies of the matrix in memory at once (x, row(x) and col(x)). It uses the matrix index idea instead.
If you were going to be doing this a lot, you would be better writing a function `Diag<-`, say, which places a vector at a specified diagonal position of a given matrix. This is an easy exercise for the reader!
Bill Venables
http://www.cmis.csiro.au/bill.venables/
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Sundar Dorai-Raj
Sent: Thursday, 12 March 2009 10:00 AM
To: Stu Field
Cc: r-help at r-project.org
Subject: Re: [R] Matrix Construction; Subdiagonal
You can always write your own function:
myDiag <- function(x, vec, k) {
x[row(x) == col(x) - k] <- vec
x
}
myDiag(A, vec, -1)
Of course, you should probably do some input checking too.
--sundar
On Wed, Mar 11, 2009 at 4:57 PM, Stu Field <sgf at colostate.edu> wrote:
Sure, that'll work fine, thanks. But I guess I was looking for something more similar to MatLab, I'm really surprised R doesn't have a preset command for this (?) Thanks again, Stu On 11 . Mar . 2009, at 5:49 PM, Sundar Dorai-Raj wrote: Does this help? A <- matrix(0, 6, 6) vec <- 1:5 A[row(A) == col(A) + 1] <- vec --sundar On Wed, Mar 11, 2009 at 4:42 PM, Stu Field <sgf at colostate.edu> wrote: I'm trying to enter a vector into the subdiagonal of a matrix but cannot find a command in R which corresponds to the MatLab version of diag(vec, k), where vec = the vector of interest, and k = the diagonal (k=0 for the diagonal; k=-1 for the subdiagonal; k=1 for superdiagonal, etc.) Is there an equivalent command in R? I'm looking for something like this: vec = seq(1, 5, 1) ? ? ? ?# vector of interest A = xyz(vec,-1) ? ? ? ? ? # creates a 6x6 matrix with vec on the subdiagonal where xyz is some function similar to diag, but with differing arguments. I can't believe there is not a simple way to do this... Thanks for your help, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Stu Field, PhD Postdoctoral Fellow Department of Biology Colorado State University 1878 Campus Delivery Fort Collins, CO 80523-1878 Office: E208 Anatomy/Zoology Phone: (970) 491-5744 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Stu Field, PhD Postdoctoral Fellow Department of Biology Colorado State University 1878 Campus Delivery Fort Collins, CO 80523-1878 Office: E208 Anatomy/Zoology Phone: (970) 491-5744 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090311/0692674e/attachment-0002.pl>
1 day later
On Wed, Mar 11, 2009 at 11:49 PM, Sundar Dorai-Raj <sdorairaj at gmail.com> wrote:
Does this help? A <- matrix(0, 6, 6) vec <- 1:5 A[row(A) == col(A) + 1] <- vec
Maybe, more simply: A <- matrix(0, 6, 6) vec <- 1:5 diag(A[-1,]) <- vec Paul