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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:53:19+00:00 2026-06-03T22:53:19+00:00

Given a list of two lists, I am trying to obtain, without using for

  • 0

Given a list of two lists, I am trying to obtain, without using for loops, a list of all element-wise products of the first list with the second. For example:

> a <- list(c(1,2), c(2,3), c(4,5))
> b <- list(c(1,3), c(3,4), c(6,2))
> c <- list(a, b)

The function should return a list with 9 entries, each of size two. For example,

> answer
[[1]]
[1] 1 6

[[2]]
[1] 3 8

[[3]]
[1] 6 4

[[4]]
[1] 2 9

[[5]]
[1] 6 12

etc...

Any suggestions would be much appreciated!

  • 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-03T22:53:20+00:00Added an answer on June 3, 2026 at 10:53 pm

    Have no idea if this is fast or memory intensive just that it works, Joris Meys’s answer is more eloquent:

    x <- expand.grid(1:length(a), 1:length(b))
    x <- x[order(x$Var1), ]    #gives the order you asked for
    FUN <- function(i)  diag(outer(a[[x[i, 1]]], b[[x[i, 2]]], "*"))
    sapply(1:nrow(x), FUN)     #I like this out put
    lapply(1:nrow(x), FUN)     #This one matches what you asked for
    

    EDIT: Now that Brian introduced benchmarking (which I love (LINK)) I have to respond. I actually have a faster answer using what I call expand.grid2 that’s a lighter weight version of the original that I stole from HERE. I was going to throw it up before but when I saw how fast Joris’s is I figured why bother, both short and sweet but also fast. But now that Diggs has dug I figured I’d throw up here the expand.grid2 for educational purposes.

    expand.grid2 <-function(seq1,seq2) {
        cbind(Var1 = rep.int(seq1, length(seq2)), 
        Var2 = rep.int(seq2, rep.int(length(seq1),length(seq2))))
    }
    
    x <- expand.grid2(1:length(a), 1:length(b))
    x <- x[order(x[,'Var1']), ]    #gives the order you asked for
    FUN <- function(i)  diag(outer(a[[x[i, 1]]], b[[x[i, 2]]], "*"))
    lapply(1:nrow(x), FUN)
    

    Here’s the results (same labeling as Bryan’s except TylerEG2 is using the expand.grid2):

    Unit: microseconds
                expr      min       lq   median       uq      max
    1   DiggsL(a, b) 5102.296 5307.816 5471.578 5887.516 70965.58
    2   DiggsM(a, b)  384.912  428.769  443.466  461.428 36213.89
    3    Joris(a, b)   91.446  105.210  123.172  130.171 16833.47
    4 TylerEG2(a, b)  392.377  425.503  438.100  453.263 32208.94
    5   TylerL(a, b) 1752.398 1808.852 1847.577 1975.880 49214.10
    6   TylerM(a, b) 1827.515 1888.867 1925.959 2090.421 75766.01
    7 Wojciech(a, b) 1719.740 1771.760 1807.686 1924.325 81666.12
    

    And if I take the ordering step out I can squeak out even more but it still isn’t close to Joris’s answer.

    enter image description here

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

Sidebar

Related Questions

For more than two days now, I'm trying to grab a list of all
I'm trying to break up an unordered list containing eight items into two lists,
In Clojure, I want to combine two lists to give a list of pairs,
For example, given the list ['one', 'two', 'one'] , the algorithm should return True
Given two datetimes ( start_date and end_date ), I'd like to generate a list
Circumstances I have a procedure which will construct a matrix using the given list
Given a list a = range(10) You can slice it using statements such as
I'm trying to remove of all members of a list appearing after a sublist
I am trying to join two tables in SQL, one containing a list of
I'm trying to compile an SWC file from a list of given ActionScript classes.

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.