matlab-like element-wise operations (broadcasting) on non-conformable arrays in r

Viewed 51

What's the most straightforward way to replicate the following MATLAB behavior in R?

reshape(1:6, [3, 1, 2]) - reshape(6:-1:1, [1, 3, 2])

ans(:,:,1) =

    -5    -4    -3
    -4    -3    -2
    -3    -2    -1


ans(:,:,2) =

     1     2     3
     2     3     4
     3     4     5

Obviously non-conformable in R:

array(1:6, dim = c(3, 1, 2)) - array(6:1, dim = c(1, 3, 2))
Error in array(1:6, dim = c(3, 1, 2)) - array(6:1, dim = c(1, 3, 2)) : 
  non-conformable arrays
0 Answers
Related