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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:41:31+00:00 2026-05-26T11:41:31+00:00

What would experienced R developers consider the most efficient (yet still readable) way to

  • 0

What would experienced R developers consider the most efficient (yet still readable) way to construct a matrix with a given number of rows and columns from a given function, such that e.g. A_ij = someFun(i,j) with 1 <= i <= rows, 1 <= j <= cols?

Since I couldn’t find something in the documentation I came up with

initMatrix <- function(rows, cols, fn) {
    A <- matrix(nrow=rows, ncol=cols)
    for (i in 1:rows)
        for (j in 1:cols)
            A[i,j] <- fn(i,j)
    return(A)
}

which seems silly and slow to me. Any improvements (particularly one-liners) welcome! 🙂

  • 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-26T11:41:32+00:00Added an answer on May 26, 2026 at 11:41 am

    I think you’re looking for outer(seq(rows),seq(cols),fn) (or as suggested below, outer(seq_len(rows),seq_len(cols),fn): would need some examples to see how much difference that made).

    You can gain a lot in readability (at least if you don’t have to go look up ?outer to find out what’s going on) this way, but I don’t actually think you save much time. Something cleverer and more efficient might be possible if your fn is vectorized to begin with: is it?

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

Sidebar

Related Questions

I would like to ask how other more experienced Coffeescript developers are using their
I am a somewhat experienced Rails developer and I thought I would try out
I would like to hear of the experiences of classic vb developers who migrated
Apple clearly has this automated in some way, I would like to have access
I want to check with some of the more experienced Oracle developers here on
Curious about how developers more experienced with Django than I might handle this: I
I need advice from experienced developers regarding the software architecture of a project. The
My team is starting a new project in silverlight. We are all experienced developers.
Hi brothers and sisters, I am wondering if anyone would share their experience on
I would like to know your experience when you need to take over somebody

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.