On Wed, Mar 11, 2009 at 11:49 PM, Sundar Dorai-Raj 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
__
R-help@r-project.org ma
s.csiro.au/bill.venables/
>
>
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org
> ] On Behalf Of Sundar Dorai-Raj
> Sent: Thursday, 12 March 2009 10:00 AM
> To: Stu Field
> Cc: r-help@r-project.org
> Subject: Re: [R] Matr
f Of Sundar Dorai-Raj
Sent: Thursday, 12 March 2009 10:00 AM
To: Stu Field
Cc: r-help@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,
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)
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 wrote:
> Sure, that'll work fine, thanks.
> But I gu
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
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 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), w
7 matches
Mail list logo