Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:31:41+00:00 2026-06-13T17:31:41+00:00

I have a function of two scalar values x_i , y_j . I have

  • 0

I have a function of two scalar values x_i, y_j. I have a vector of n x_i values, X and n y_j values, e.g.

myfunction <- function(x,y) min(x,y)
X <- 1:3
Y <- 2:4

I want to fill out the $n$ by $n$ matrix whose elements (i,j) are given by myfunction(x_i, y_j). There’s a lot of ways to do this in R, and I’m curious about their relative performance.

For instance, this seems like a task for outer, but it seems to get confused whether it is passing a vector or scalar to myfunction. First consider:

r
outer(X, Y, paste)

gives me each of the pairs

     [,1]  [,2]  [,3] 
[1,] "1 2" "1 3" "1 4"
[2,] "2 2" "2 3" "2 4"
[3,] "3 2" "3 3" "3 4"

Looks good. But

outer(X, Y, myfunction)

throws the error:

Error: dims [product 9] do not match the length of object [1]

Meanwhile other possible functions seem to behave as I expected with scalars, such as:

myfunction <- function(x,y) exp((x-y)^2)

which works fine

outer(X, Y, myfunction)


         [,1]      [,2]        [,3]
[1,] 2.718282 54.598150 8103.083928
[2,] 1.000000  2.718282   54.598150
[3,] 2.718282  1.000000    2.718282

In a few quick numerical experiments, it seems this is slightly faster than expand.grid, and the function call more compact, but I don’t seem to understand why some functions appear to work as I anticipate and others do not.

The classic expand.grid solution also requires the function to work with vector arguments, which means a very different thing for my example with min; a different version of the same problem. Is there a way to enforce the fact that the arguments to my function must be scalars rather than vectors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:31:42+00:00Added an answer on June 13, 2026 at 5:31 pm

    The function passed to outer must be vectorized,

    From the source code

       FUN <- match.fun(FUN)
            Y <- rep(Y, rep.int(length(X), length(Y)))
            if (length(X)) 
                X <- rep(X, times = ceiling(length(Y)/length(X)))
            robj <- FUN(X, Y, ...)
            dim(robj) <- c(dX, dY)
    

    min will return a single number, you would want pmin.

    More generally you could use Vectorize.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function with two input variables min.depth<-2 max.depth<-5 the function produces a
I have a function that returns two values in a list. Both values need
I have a scalar function that takes a two variables @input1 @input2 and it
say I have a scalar-valued function with takes two Guids as parameters: dbo.CarDistribution The
I have a base class, Parameter, and two derived classes: Scalar & Vector. In
I have two function and I need to execut in one onClick . The
I have a function with two optional parameters: def func(a=0, b=10): return a+b Somewhere
I have a function which takes two arguments, the ID of an item (wine)
Suppose I have two constructor function: var Person = function(xx,xx,xxx,xxxxxxx) { //Person initialization }
I have a function that creates time intervals between two time marks. The function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.