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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:01:30+00:00 2026-05-31T02:01:30+00:00

Imagine I have a function that gives the transition probability of going from state

  • 0

Imagine I have a function that gives the transition probability of going from state {x,y} to state {X, Y}: transition <- function(x,y,X,Y)

Imagine the x values can assume values in on a discrete set of points x_grid and y assume discrete values in y_grid, and I’d like to compute all possible transitions, e.g. fill out as a 2D matrix like this:

      X1Y1 X2Y1 X3Y1 X1Y2 .... X3Y3 
x1,y1
x2,y1
x3,y1
x1,y2
x2,y2
x3,y2
...
x3,y3

What’s the simplest way to loop over my function in R to generate this matrix?

A cumbersome approach with for loops

x_grid <- 1:3
y_grid <- 1:3

## dummy function
transition <- function(x,y,X,Y)
    x == X && y == Y

nx <- length(x_grid)
ny <- length(y_grid)
T <- matrix(NA, ncol = nx * ny, nrow = nx * ny)
for(i in 1:nx)
  for(j in 1:ny)
    for(k in 1:nx)
      for(l in 1:ny)
        T[i+(j-1)*ny, k+(l-1)*ny] <- 
          transition(x_grid[i], y_grid[j], x_grid[k], y_grid[l])

Surely there’s a more efficient and more elegant way to do this in R?

For instance,

sapply(x_grid, function(x) 
  sapply(y_grid, function(y) 
   sapply(x_grid, function(X) 
     sapply(y_grid, function(Y) 
       transition(x,y,X,Y) )))) 

works more efficiently but returns an object of the wrong shape. Turning the outermost apply into an lapply and then doing cbind on it’s elements corrects this, but feels very crude.

  • 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-31T02:01:32+00:00Added an answer on May 31, 2026 at 2:01 am

    Here’s a wild shot in the dark. I hope it’s helpful:

    #Some simple data grid points
    d <- expand.grid(1:3,1:3,1:3,1:3)
    #Trivial function
    f <- function(x,y,X,Y){x*y*X*Y}
    #Wrap mapply in matrix; fills by column by default
    matrix(mapply(f,d$Var1,d$Var2,d$Var3,d$Var4),nrow = 9)
          [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
     [1,]    1    2    3    2    4    6    3    6    9
     [2,]    2    4    6    4    8   12    6   12   18
     [3,]    3    6    9    6   12   18    9   18   27
     [4,]    2    4    6    4    8   12    6   12   18
     [5,]    4    8   12    8   16   24   12   24   36
     [6,]    6   12   18   12   24   36   18   36   54
     [7,]    3    6    9    6   12   18    9   18   27
     [8,]    6   12   18   12   24   36   18   36   54
     [9,]    9   18   27   18   36   54   27   54   81
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a function that looks like this: @SuppressWarnings(unchecked) public static <E> Set<E>
Imagine I have an function which goes through one million/billion strings and checks smth
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
Imagine I have a class Window with a member function show which causes the
Imagine I have these python lists: keys = ['name', 'age'] values = ['Monty', 42,
Imagine I have a process that starts several child processes. The parent needs to
I have a query that can be summarised in SQL as follows; Select S.StockCode
I have a viewModel with an observableArray that gets updated from the server, then
I have an iOS app that displays content from a sqlite db in a

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.