On Wed, 12 Sep 2007, Robin Hankin wrote:
> Hello
>
>
> I have X, an n-by-n matrix and want to convert it to Y, an
> n(n-1)/2 -by- n matrix such that each row of Y
> corresponds to an element of the upper diagonal
> of X. Say row k of Y corresponds to [i,j] with i\neq j.
>
> Then Y[i,k] = X[i,j
you could try the following:
n <- nrow(X)
k <- n * (n - 1) / 2
Y <- matrix(NA, k, n)
ind.up <- which(upper.tri(X), arr.ind = TRUE)
ind.lo <- which(lower.tri(X), arr.ind = TRUE)
Y[cbind(1:k, ind.lo[, 1])] <- X[ind.up]
Y[cbind(1:k, ind.lo[, 2])] <- X[ind.lo]
Y
I hope it helps.
Best,
Dimitris
--
2 matches
Mail list logo