R: Convert upper triangular part of a matrix to symmetric matrix

Lila

I have the upper triangular part of matrix in R (without diagonal) and want to generate a symmetric matrix from the upper triangular part (with 1 on the diagonal but that can be adjusted later). I usually do that like this:

res.upper <- rnorm(4950)
res <- matrix(0, 100, 100)
res[upper.tri(res)] <- res.upper
rm(res.upper)
diag(res) <- 1
res[lower.tri(res)]  <- t(res)[lower.tri(res)]

This works fine but now I want to work with very large matrices. Thus, I would want to avoid having to store res.upper and res (filled with 0) at the same time. Is there any way I can directly convert res.upper to a symmetric matrix without having to initialize the matrix res first?

李哲源

I think there are two issues here.

now I want to work with very large matrices

Then do not use R code to do this job. R will use much more memory than you expect. Try the following code:

res.upper <- rnorm(4950)
res <- matrix(0, 100, 100)
tracemem(res)  ## trace memory copies of `res`
res[upper.tri(res)] <- res.upper
rm(res.upper)
diag(res) <- 1
res[lower.tri(res)]  <- t(res)[lower.tri(res)]

This is what you will get:

> res.upper <- rnorm(4950)  ## allocation of length 4950 vector
> res <- matrix(0, 100, 100)  ## allocation of 100 * 100 matrix
> tracemem(res)
[1] "<0xc9e6c10>"
> res[upper.tri(res)] <- res.upper
tracemem[0xc9e6c10 -> 0xdb7bcf8]: ## allocation of 100 * 100 matrix
> rm(res.upper)
> diag(res) <- 1
tracemem[0xdb7bcf8 -> 0xdace438]: diag<-  ## allocation of 100 * 100 matrix
> res[lower.tri(res)]  <- t(res)[lower.tri(res)]
tracemem[0xdace438 -> 0xdb261d0]: ## allocation of 100 * 100 matrix
tracemem[0xdb261d0 -> 0xccc34d0]: ## allocation of 100 * 100 matrix

In R, you have to use 5 * (100 * 100) + 4950 double words to finish these operations. While in C, you only need at most 4950 + 100 * 100 double words (In fact, 100 * 100 is all that is needed! Will talk about it later). It is difficult to overwrite object directly in R without extra memory assignment.

Is there any way I can directly convert res.upper to a symmetric matrix without having to initialize the matrix res first?

You do have to allocate memory for res because that is what you end up with; but there is no need to allocate memory for res.upper. You can initialize the upper triangular, while filling in the lower triangular at the same time. Consider the following template:

#include <Rmath.h>  // use: double rnorm(double a, double b)
#include <R.h>  // use: getRNGstate() and putRNGstate() for randomness
#include <Rinternals.h>  // SEXP data type

## N is matrix dimension, a length-1 integer vector in R
## this function returns the matrix you want
SEXP foo(SEXP N) {
  int i, j, n = asInteger(N);
  SEXP R_res = PROTECT(allocVector(REALSXP, n * n));  // allocate memory for `R_res`
  double *res = REAL(R_res);
  double tmp;  // a local variable for register reuse
  getRNGstate();
  for (i = 0; i < n; i++) {
    res[i * n + i] = 1.0;  // diagonal is 1, as you want
    for (j = i + 1; j < n; j++) {
      tmp = rnorm(0, 1);  
      res[j * n + i] = tmp; // initialize upper triangular
      res[i * n + j] = tmp;  // fill lower triangular
      }
    }
  putRNGstate();
  UNPROTECT(1);
  return R_res;
  }

The code has not been optimized, as using integer multiplication j * n + i for addressing in the innermost loop will result in performance penalty. But I believe you can move multiplication outside the inner loop, and only leave addition inside.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

convert triangular parwise matrix to symmetric matrix with 0 diagonal R

From Dev

Convert upper triangular part of a matrix to 3-column long format

From Dev

Convert a matrix in R into a upper triangular/lower triangular matrix with those corresponding entries

From Dev

Outer function in R for upper triangular matrix

From Dev

Upper Triangular Matrix in Scala

From Dev

Upper Triangular Matrix in Scala

From Dev

transform the upper/lower triangular part of a symmetric matrix (2D array) into a 1D array and return it to the 2D format

From Dev

Check Upper or Lower Triangular Matrix

From Dev

Linear index upper triangular matrix

From Dev

Convert vector into a triangular matrix

From Dev

Convert vector into a triangular matrix

From Dev

Get upper triangular non-zero elements of a sparse matrix in R

From Dev

Get upper triangular non-zero elements of a sparse matrix in R

From Dev

Create a horizontically stretched upper triangular matrix

From Dev

Melt the Upper Triangular Matrix of a Pandas Dataframe

From Dev

Scala fast generation of upper triangular matrix coordinates

From Dev

Map upper triangular matrix on vector skipping the diagonal

From Dev

How to create a specific upper triangular matrix?

From Dev

How to create a specific upper triangular matrix?

From Dev

Reshape / Transform an upper triangular matrix in MATLAB

From Dev

Creating a symmetric matrix in R

From Dev

Constructing symmetric matrix on R

From Dev

Numpy: convert an array to a triangular matrix

From Dev

Numpy: Efficient way to convert indices of a square matrix to its upper triangular indices

From Dev

transfer the lower triangular part of a matrix into a vector in matlab

From Dev

What is the best data structure for representing an upper triangular matrix in Java?

From Dev

Make a numpy upper triangular matrix padded with Nan instead of zero

From Dev

filling an upper triangular matrix (including diagonal) with a vector using RcppArmadillo

From Dev

Finding upper/lower triangular form of arbitrary matrix n*n - python

Related Related

  1. 1

    convert triangular parwise matrix to symmetric matrix with 0 diagonal R

  2. 2

    Convert upper triangular part of a matrix to 3-column long format

  3. 3

    Convert a matrix in R into a upper triangular/lower triangular matrix with those corresponding entries

  4. 4

    Outer function in R for upper triangular matrix

  5. 5

    Upper Triangular Matrix in Scala

  6. 6

    Upper Triangular Matrix in Scala

  7. 7

    transform the upper/lower triangular part of a symmetric matrix (2D array) into a 1D array and return it to the 2D format

  8. 8

    Check Upper or Lower Triangular Matrix

  9. 9

    Linear index upper triangular matrix

  10. 10

    Convert vector into a triangular matrix

  11. 11

    Convert vector into a triangular matrix

  12. 12

    Get upper triangular non-zero elements of a sparse matrix in R

  13. 13

    Get upper triangular non-zero elements of a sparse matrix in R

  14. 14

    Create a horizontically stretched upper triangular matrix

  15. 15

    Melt the Upper Triangular Matrix of a Pandas Dataframe

  16. 16

    Scala fast generation of upper triangular matrix coordinates

  17. 17

    Map upper triangular matrix on vector skipping the diagonal

  18. 18

    How to create a specific upper triangular matrix?

  19. 19

    How to create a specific upper triangular matrix?

  20. 20

    Reshape / Transform an upper triangular matrix in MATLAB

  21. 21

    Creating a symmetric matrix in R

  22. 22

    Constructing symmetric matrix on R

  23. 23

    Numpy: convert an array to a triangular matrix

  24. 24

    Numpy: Efficient way to convert indices of a square matrix to its upper triangular indices

  25. 25

    transfer the lower triangular part of a matrix into a vector in matlab

  26. 26

    What is the best data structure for representing an upper triangular matrix in Java?

  27. 27

    Make a numpy upper triangular matrix padded with Nan instead of zero

  28. 28

    filling an upper triangular matrix (including diagonal) with a vector using RcppArmadillo

  29. 29

    Finding upper/lower triangular form of arbitrary matrix n*n - python

HotTag

Archive