Using outer in R on a matrix valued function

Viewed 53

Can outer be used to build a block matrix from a matrix valued function?

For example define:

f = function(x,y){outer(x,y)} 

(I want to generalize to when f outputs an arbitrary matrix not just the outer product) Then I want to create the block matrix created from evaluating f on a list of 2d vectors

For example:

input =     
[,1] [,2] [,3] [,4]
x    1    1    2    2
y    1    2    1    2

The output would then be a 4x4 matrix:

1 1 1 2
1 1 1 2
1 1 4 4
2 2 4 4

Where for example the upper left block is f((1,1),(1,1)).

I thought outer(input,input,f) might work but it says the dimensions don't match.

0 Answers
Related