62432

Multiply array with different dimension

Question:

I wanted to multiply(or divide) two arrays with different dimensions (a1=[48,38,31] and b1[48,38]). So far, I was using a for loop over the third dimension. However, I was wondering how to use (if it's possible) apply for that. Let's say I have the following samples:

a1<- array(rnorm(20), dim=c(2,3,3)) b1<- array(rnorm(20), dim=c(2,3))

If I tried to do directly a1/b1 (or *) I can't because they need to have the same dimensions. So I used a for loop:

for(i in 1:3){ m1[,,i] <- a1[,,i]/b1 }

But I would like to avoid the use of the loop.

Answer1:

One option would be

array(c(a1)/rep(b1, dim(a1)[3]), dim= dim(a1))

Or we can use apply

apply(a1, 3, FUN=function(x) x/b1)

Answer2:

Alternatively, you can use sweep function which is designed to do this kind of operation.

a1<- array(rnorm(20), dim=c(2,3,3)) b1<- array(rnorm(20), dim=c(2,3)) m1 <- array(0, dim=c(2,3,3)) # original solution for(i in 1:3){ m1[,,i] <- a1[,,i]/b1 } # apply sweep # to avoid the warning info add 'check.margin=F' m2 <- sweep(a1, 1, b1, "/", check.margin=F) all.equal(m1, m2) #[1] TRUE

Recommend

  • How to use discontinuous range in SUMIF
  • Display p-values under 0.1 in r stargazer
  • How to extract the p-value for the slope from an ols object in R
  • How to store pointer to S3 objects in Amazon SimpleDB?
  • R ggvis interactive slider for calculating y values (e.g. for background correction)
  • why doesn't this visNetwork in R show edge
  • Imagemagick set interline spacing?
  • Finding number of samples in a .wav header
  • How to model a mixture of finite components from different parametric families with JAGS?
  • Is mp4 stream able with ffserver?
  • What is this strange character in chrome's resource css viewer?
  • Geom_jitter colour based on values
  • Double-click autofill - dynamic based on adjacent cell
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • Excel VBA URLDownloadToFile Error for https Ressource
  • Creating a Multidimensional, Associative Array in VBScript
  • Conversion from string “a” to type 'Boolean' is not valid
  • Reduction and collapse clauses in OMP have some confusing points
  • how to avoid repetitive constructor in children
  • How to get Eclipse Oxygen to run on Java 9
  • How do I access an unhandled exception in an MVC Error view?
  • MongoDb aggregation
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Google Custom Search with transparent background
  • Control modification in presentation layer
  • Knitr HTML Loop - Some HTML output, some R output
  • vba code to select only visible cells in specific column except heading
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Python: how to group similar lists together in a list of lists?
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • Understanding cpu registers
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize