[R] Algorithm needed

hbeltra at sas.upenn.edu hbeltra at sas.upenn.edu
Thu Nov 9 22:19:29 CET 2006


I have a matrix of size "n" and I want to create a new one in which the columns
are sums of the original matrix, with some order in the sums.  For example, if
matrix A has 4 columns, then the new matrix should have 6 columns with the
following info from the columns of A: 1+2, 1+3, 1+4, 2+3, 2+4, 3+4. If matrix A
has 5 columns, then the new matrix has 10 columns: 1+2, 1+3, 1+4, 1+5, 2+3, 2+4,
2+5, 3+4, 3+5, 4+5

I thought of using a for loop:
for (i in 1:n-1) {
  for (j in (i+1):n) {
      A[,i] + A[,j]
  }
}

but I don't know how to store the results so the new matrix has all the columns.
 I know the number of columns in the new matrix is given by n(n-1)/2.

Any ideas?  Thanks.

Hiram



More information about the R-help mailing list