Search code examples
rpsych

Finding out harmonic means based on two matrices and store results in third matrix


I have two matrices a and b

a = matrix(1:4,ncol=2)
b = matrix(5:8,ncol=2)

Now i want to find out harmonic means of these and put in a third matrix.

An example :-

 x = a[1,1],y = b[1,1] then harmonic mean of x and y = 2*x*y/(x+y)

Desired Output:-

c = matrix(c(1.666667,4.2,3,5.333333),ncol = 2)

Solution

  • Since arithmetic operators work elementwise on matrices should be:

    2*x*y/(x+y)
    

    .. or the obvious substitution.