pachadotdev
pachadotdev

Reputation: 3775

Efficient way to obtain matrix with pairwise maximum between two values

I want to create a matrix that for the entry i,j it returns the maximum between D[i,1] and D[j,1].

I have a vector of numbers that in a MWE can be reduced to this:

set.seed(10)
n <- 2000 
D <- matrix(runif(n,0,100), ncol=1)

With a double for loop in Base R it is extremely inefficient:

X <- Matrix::Matrix(0, nrow = n, ncol = n, sparse = T)

for (i in 1:n) {
  for (j in 1:n) {
    X[i,j] <- max(D[i,1], D[j,1])
  }
}

I also tried with dplyr:

library(dplyr)

X <- tibble(i = 1:n, D = D)

X <- expand.grid(i = 1:n, j = 1:n)

X <- X %>%
  as_tibble() %>%
  left_join(X, by = "i") %>%
  left_join(X, by = c("j" = "i")) %>%
  rowwise() %>%
  mutate(D = max(D.x, D.y)) %>%
  ungroup()

it returns Error: std::bad_alloc before I can do X <- Matrix::Matrix(X$D, nrow = n, ncol = n, sparse = T)

My last try was to use RcppArmadillo in a way it also works with Windows:

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;

// [[Rcpp::export]]
arma::mat pairwise_max(arma::mat x, arma::mat y) {
  // Constants
  int n = (int) x.n_rows;

  // Output
  arma::mat z(n,n);

  // Filling with ones
  z.ones();

  for (int i=0; i<n; i++)
    for (int j=0; j<=i; j++) {
      // Fill the lower part
      z.at(i,j) = std::max(y(i,0), y(j,0));
      // Fill the upper part
      z.at(j,i) = z.at(i,j);
    }

    return z;
}

it works almost flawlessy, but I'm quite sure there is an efficient way with base R that I am not seeing.

Upvotes: 0

Views: 228

Answers (1)

F. Priv&#233;
F. Priv&#233;

Reputation: 11728

In base R, I would do

D2 <- drop(D)
X2 <- outer(D2, D2, pmax)

which is ~20 times as fast as the double for loop.

Upvotes: 2

Related Questions