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 5976463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:13:57+00:00 2026-05-22T21:13:57+00:00

Consider matrices d and r with dim(d) = J x D and dim(r) =

  • 0

Consider matrices d and r with dim(d) = J x D and dim(r) = J x R.
Let fun(a, b) be a function that takes two vectors of the same length and returns some number.
I want to treat the columns of d and r respectively as my units of interest and apply outer to them.

The following code accomplishes this by creating lists of the columns of d and r and then using both outer and sapply:

d.cols <- split(d, col(d))
r.cols <- split(r, col(r))
outer(d.cols, r.cols,
      function(x,y) {
           sapply(seq_along(x),
                 function(i) {
                     Fun(x[[i]], y[[i]]) })} )

The code does what I want and is relatively efficient, but is clumsy and unclear. Is there a better way to accomplish what I am trying to get at?

  • 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-05-22T21:13:58+00:00Added an answer on May 22, 2026 at 9:13 pm

    You are pretty close. As described in this related question, all you need is the Vectorize() function to convert your Fun() function into a vectorized version:

    VecFun <- Vectorize( Fun )
    

    Then you can simply do:

    outer(d.cols, r.cols, VecFun )
    

    E.g. if you define

    Fun <- function(a,b) sum(a+b)
    

    and r,d matrices are defined as follows:

    J <- 5
    D <- 3
    R <- 4
    
    d <- matrix( 1:(J*D), J, D)
    r <- matrix( 1:(J*R), J, R)
    

    then you get this:

    > outer(d.cols, r.cols, VecFun)
    
       1   2   3   4
    1 30  55  80 105
    2 55  80 105 130
    3 80 105 130 155
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two matrices A and B with same number of rows. Consider a
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider this scenario. I have some business logic that now and then will be
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this example: class MyClass: def func(self, name): self.name = name I know that
Consider two forms: form1 and form2 form1 calls form2 : form2 frm = new
Consider the following two enums: enum MyEnum1 { Value1 = 1, Value2 = 2,
Consider: char [] chararray = txt1.Text; How we do the same in Visual Basic
I have two matrices I want to perform a loop on. My problem is
I have a static class 'Defaults' which shall hold default matrices that are forwarded

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.