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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:32:34+00:00 2026-06-05T03:32:34+00:00

Given a matrix M and smaller matrices with different possible values, I seek to

  • 0

Given a matrix M and smaller matrices with different possible values, I seek to list all possible matrices resulting from superimposing combinations of these small matrices into the matrix M. The small matrices are to be inserted in the locations of M having the same row/column names.

For instance, say have:

 M <- matrix(rep(0, 49), nrow =7, ncol =7)
 rownames(M) <- colnames(M) <-seq(1,7)
 > M
 1 2 3 4 5 6 7
 1 0 0 0 0 0 0 0
 2 0 0 0 0 0 0 0
 3 0 0 0 0 0 0 0
 4 0 0 0 0 0 0 0
 5 0 0 0 0 0 0 0
 6 0 0 0 0 0 0 0
 7 0 0 0 0 0 0 0

# Generate first set of small matrices:
sub_mat_1_1 <- matrix(rep(1, 9), nrow =3, ncol =3)
rownames(sub_mat_1_1) <- colnames(sub_mat_1_1) <- c(2,3,5)
sub_mat_1_2 <- matrix(rep(2, 9), nrow =3, ncol =3)
rownames(sub_mat_1_2) <- colnames(sub_mat_1_2) <- c(2,3,5)
sub_mat_1_3 <- matrix(rep(3, 9), nrow =3, ncol =3)
rownames(sub_mat_1_3) <- colnames(sub_mat_1_3) <- c(2,3,5)
submatrix_1 <- list(sub_mat_1_1, sub_mat_1_2, sub_mat_1_3)

# Generate second set of small matrices:
submatrix_2 <- list()
sub_mat_2_1 <- matrix(rep(1, 4), nrow =2, ncol =2)
rownames(sub_mat_2_1) <- colnames(sub_mat_2_1) <- c(1,6)
sub_mat_2_2 <- matrix(rep(2, 4), nrow =2, ncol =2)
rownames(sub_mat_2_2) <- colnames(sub_mat_2_2) <- c(1,6)
submatrix_2 <- list(sub_mat_2_1, sub_mat_2_2)

# Generate list of small matrices:
submatrices <- list()
submatrices[[1]] <- submatrix_1
submatrices[[2]] <- submatrix_2

[[1]]
[[1]][[1]]
  2 3 5
2 1 1 1
3 1 1 1
5 1 1 1

[[1]][[2]]
  2 3 5
2 2 2 2
3 2 2 2
5 2 2 2

[[1]][[3]]
  2 3 5
2 3 3 3
3 3 3 3
5 3 3 3


[[2]]
[[2]][[1]]
  1 6
1 1 1
6 1 1

[[2]][[2]]
  1 6
1 2 2
6 2 2

Since there are 3 possibilities for the first small matrix set and 2 for the second, I seek to output, without using a for loop, all 6 possible matrices as a list:

[[1]]
  1 2 3 4 5 6 7
1 1 0 0 0 0 1 0
2 0 1 1 0 1 0 0
3 0 1 1 0 1 0 0
4 0 0 0 0 0 0 0
5 0 1 1 0 1 0 0
6 1 0 0 0 0 1 0
7 0 0 0 0 0 0 0

[[2]]
  1 2 3 4 5 6 7
1 1 0 0 0 0 1 0
2 0 2 2 0 2 0 0
3 0 2 2 0 2 0 0
4 0 0 0 0 0 0 0
5 0 2 2 0 2 0 0
6 1 0 0 0 0 1 0
7 0 0 0 0 0 0 0

[[3]]
  1 2 3 4 5 6 7
1 1 0 0 0 0 1 0
2 0 3 3 0 3 0 0
3 0 3 3 0 3 0 0
4 0 0 0 0 0 0 0
5 0 3 3 0 3 0 0
6 1 0 0 0 0 1 0
7 0 0 0 0 0 0 0

[[4]]
  1 2 3 4 5 6 7
1 2 0 0 0 0 2 0
2 0 1 1 0 1 0 0
3 0 1 1 0 1 0 0
4 0 0 0 0 0 0 0
5 0 1 1 0 1 0 0
6 2 0 0 0 0 2 0
7 0 0 0 0 0 0 0

[[5]]
  1 2 3 4 5 6 7
1 2 0 0 0 0 2 0
2 0 2 2 0 2 0 0
3 0 2 2 0 2 0 0
4 0 0 0 0 0 0 0
5 0 2 2 0 2 0 0
6 2 0 0 0 0 2 0
7 0 0 0 0 0 0 0

[[6]]
  1 2 3 4 5 6 7
1 2 0 0 0 0 2 0
2 0 3 3 0 3 0 0
3 0 3 3 0 3 0 0
4 0 0 0 0 0 0 0
5 0 3 3 0 3 0 0
6 2 0 0 0 0 2 0
7 0 0 0 0 0 0 0

In general, I may have n given “lists of small matrices”, each with its own number of matrices. How would I use an apply type function in this context?

  • 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-05T03:32:35+00:00Added an answer on June 5, 2026 at 3:32 am

    This gives the requested output, but I have to agree with the commenters that the problem is not very well posed. How do you define the positions into which the values of the submatrices are inserted? I simply assumed that you wanted them inserted into a particular subset of rows and columns as shown in your requested output …

    A function to insert two matrices into specified rows/columns of M (defined globally, probably bad practice)

    tmpmatf <- function(m1,m2,rc1=c(2,3,5),rc2=c(1,6)) {
        pos1 <- as.matrix(expand.grid(rc1,rc1))
        pos2 <- as.matrix(expand.grid(rc2,rc2))
        M[pos1] <- m1
        M[pos2] <- m2
        M
    }
    

    Now use expand.grid to create a data frame with all combinations of indices from each list of submatrices, and use alply (array-to-list) from plyr to run tmpmatf on each combination:

    library(plyr)
    alply(expand.grid(seq(length(submatrices[[1]])),
                      seq(length(submatrices[[2]]))),
          1,
          function(x) {
              tmpmatf(submatrices[[1]][[x[[1]]]],submatrices[[2]][[x[[2]]]])
          })
    

    This should work with an arbitrary number of submatrices in each your two lists of submatrices, but if you really have more (>2) lists of submatrices, then you haven’t given us enough information to specify how (e.g.) the third list of submatrices should be pasted into the larger matrix …

    Note that the first part of this (using matrix indexing via a two-column matrix) is a lot faster than using a for loop to insert individual elements into the matrix, but the second part (alply) is not actually much (any?) faster than a pair of nested for loops that iterate over all combinations — the latter might be clearer/easier to debug in this case …

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

Sidebar

Related Questions

I have a function that returns a comparison matrix from a given list: def
Given an n-dimensional matrix of values: what is the most efficient way of retrieving
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
I need to create a function to rotate a given matrix (list of lists)
Given the matrix structure(list(X1 = c(1L, 2L, 3L, 4L, 2L, 5L), X2 = c(2L,
Given: square matrix, and list which represents the index of rows to be removed,
Given a n*n matrix and a value k, how do we find all the
If I have a matrix given as a list of rows [[1,2,3],[4,5,6]], I want
When given a square matrix, what would be the best way to find all
I'm trying to calculate all the possible values of a grid size (x by

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.