Matlab bsxfun (@times, ..., ...) is equivalent in R

Does R have Matlab equivalent bsxfun(@times,a,b)? Suppose you want to execute element wise multiplicationon a matrix a,b:

Matlab:

a=[1 0 3 -4];
b=[0 1 5 7; 2 9 -3 4];

bsxfun(@times,a,b) = [0 0 15 -28; 2 0 -9 -16]

R:

a<-c(1,0,3,-4)
b<-matrix(c(0,2,1,9,5,-3,7,4),nrow = 2,ncol = 4)

a * b = matrix(c(0,0,3,-36,5,0,21,-16),nrow = 2,ncol = 4)

Any idea on the R path gets the results above a*b, as I expected it to be identical to Matlabbsxfun(@times,a,b)

EDIT:

bsxfun("*",repmat(a,2,1),b) # using R {pracma}

The best

+4
source share
3 answers

Do this with the main column matrices, as this is the R convention:

> b<-matrix(c(0,2,1,9,5,-3,7,4),nrow = 4,ncol = 2)
> a*b
     [,1] [,2]
[1,]    0    5
[2,]    0    0
[3,]    3   21
[4,]  -36  -16

If you take the original design b, you get an unpleasant surprise when you try to use sweep:

> b2<-matrix(c(0,2,1,9,5,-3,7,4),nrow = 2,ncol = 4)
> sweep(b2, 2, a, '*')
     [,1] [,2] [,3] [,4]
[1,]    0    0   15  -28
[2,]    2    0   -9  -16

matrix , byrow = TRUE , b - Matlab.

> b3<-matrix(c(0,2,1,9,5,-3,7,4),nrow = 2,ncol = 4, byrow=TRUE)
> sweep(b3, 2, a, '*')
     [,1] [,2] [,3] [,4]
[1,]    0    0    3  -36
[2,]    5    0   21  -16
+2

, "bsxfun" "sweep", . "bsxfun" , , .

matrix(a, ncol = n_col, nrow = nsample, byrow = TRUE) * b

sweep() , , (1e6-by-4).


# test of recycling efficiency

rm(list=ls())

library(microbenchmark)
library(pracma)

a = c(1,0,3,-4)
n_col = 4

# make example more realistic by expanding number of rows of b
nsample = 1e3
b = repmat(matrix(c(0,2,1,9,5,-3,7,4),nrow = 2,ncol = n_col), nsample / 2, 1)

print(microbenchmark(
  erg_1 = matrix(a, ncol = n_col,  nrow = nsample, byrow = TRUE) * b,
  erg_2 = matrix(rep.int(a, nsample), nrow = nsample, ncol = n_col, byrow = TRUE) * b,
  erg_3 = matrix(a * c(t(b)), nrow = nsample, ncol = n_col, byrow = TRUE),
  erg_4 = sweep(b, 2, a, '*'),
  erg_5 = bsxfun('*', repmat(a, nsample, 1), b) 
  ))


#same as above but now larger matrices
nsample = 1e6
b = repmat(matrix(c(0,2,1,9,5,-3,7,4),nrow = 2,ncol = n_col), nsample / 2, 1)

print(microbenchmark(
  erg_1 = matrix(a, ncol = n_col,  nrow = nsample, byrow = TRUE) * b,
  erg_2 = matrix(rep.int(a, nsample), nrow = nsample, ncol = n_col, byrow = TRUE) * b,
  erg_3 = matrix(a + c(t(b)), nrow = nsample, ncol = n_col, byrow = TRUE),
  erg_4 = sweep(b, 2, a, '*')
  #erg_5 = bsxfun('*', repmat(a, nsample, 1), b) #bsxfun is non-competitive
))

>Unit: microseconds
  expr      min        lq       mean    median        uq      max neval
 erg_1    9.057   10.1135   11.93394   11.0195   12.6790   36.226   100
 erg_2   14.189   15.3970   18.75324   16.9060   19.9250   41.358   100
 erg_3   26.263   28.8295   35.04538   30.9430   34.8675   86.941   100
 erg_4   40.452   44.0750   56.88289   51.4705   66.4130  109.279   100
 erg_5 2694.827 2968.4755 3243.76025 3208.9185 3417.5125 5575.306   100
>Unit: milliseconds
  expr      min       lq     mean   median       uq      max neval
 erg_1 10.85538 11.30668 20.58625 12.93408 13.28290 69.17918   100
 erg_2 16.07206 18.00058 29.17394 18.24751 20.09845 75.30993   100
 erg_3 22.41231 24.58957 30.83620 24.99544 26.49047 79.71910   100
 erg_4 20.74838 21.53673 29.52071 22.88867 23.30420 81.07150   100
+2

: , (1,10,2). , , . - - , 4 . github repo bsxfun.R ( , @g g)

MARGIN, , pracma::bsxfunοΌˆοΌ‰ sweep(). (. github), , .

tag: R, pracma, array

0

Source: https://habr.com/ru/post/1624510/


All Articles