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

  • Home
  • SEARCH
  • 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 9285977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:18:06+00:00 2026-06-18T19:18:06+00:00

Consider a matrix with r rows and c columns and containing v integers between

  • 0

Consider a matrix with r rows and c columns and containing v integers between 0 and v-1; in the following example, r=4, c=2, and v=6.

L <- c(0,1,1,2,0,1,2,3)
(x <- matrix(L,nrow=4,ncol=2,byrow = TRUE))

 ## 0 1 
 ## 1 2 
 ## 0 1 
 ## 2 3

The goal is to generate a r*c (row) by v column incidence matrix, as follows:

  • each row corresponds to one element of the original matrix (in column-major order, i.e. in the example here the 4th row corresponds to x[4,1] and the 5th row corresponds to x[1,2])
  • find the “neighbors” above and below each element, wrapping around (cyclically) from the top to the bottom of the matrix; count the number of neighbor elements for each value of v.

For example, the first element in the matrix (x[1,1]) has neighbours 1 (below) and 2 (“above”, i.e. wrapped around to the bottom of the column; thus we enter 1 in columns 2 and 3 of row 1, matching the corresponding elements of 0:(v-1). The rest of the row is set to zero:

  rownames  0  1  2  3  4  5            
  [1]       0  1  1  0  0  0

The next element (x[2,1]) has 0 on both sides (above and below), so the first column (corresponding to 0) is set to 2, with the rest of the elements equal to zero.

  [2]       2  0  0  0  0  0

The full matrix for the example above is:

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

The row sums are each 2.

  • 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-18T19:18:08+00:00Added an answer on June 18, 2026 at 7:18 pm
    L =c(0,1,1,2,0,1,2,3)
    x=matrix(L,nrow=4,ncol=2,byrow = TRUE)
    

    There might be a cleaner way to do this:

    wrapind <- function(i,n)
        ifelse((r <- i %% n) == 0, n, r)
    
    
    n <- nrow(x)
    v <- 6
    incmat <- matrix(0,ncol=v,nrow=prod(dim(x)),
                     dimnames=list(NULL,0:(v-1)))
    k <- 1
    for (i in seq(ncol(x)))
        for (j in seq(nrow(x))) {
            cat(i,j,k,"\n")  ## unnecessary
            tt <- table(as.character(x[wrapind(c(j-1,j+1),n),i]))
            incmat[k,names(tt)] <- tt
            k <- k+1
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following matrix, m <- matrix(letters[c(1,2,NA,3,NA,4,5,6,7,8)], 2, byrow=TRUE) ## [,1] [,2] [,3] [,4]
Consider the following example: dat <- matrix(rexp(240, rate=.1), ncol=6) par(mfrow=c(3,2)) for (i in 1:6){
Please, input the following code: A <- matrix(11, nrow = 4, ncol = 3)
Consider the following matrix, nc <- 5000 nr <- 1024 m <- matrix(rnorm(nc*nr), ncol=nc)
Consider the following piece of toy-code (purely illustrative): y <- data.frame(matrix(c(11,12,13,14,113,124,215,219),nrow=4)); y[,2] <- factor(y[,2]);
Consider a matrix M and a set of subscripts stored in columns I and
Consider the following matrix: sequence <- structure(list(C1 = c(2L, 9L, 3L, 9L, 1L, 8L,
Consider having a matrix. From this matrix I select the same number of elements
Consider having the following header file (c++): myclass.hpp #ifndef MYCLASSHPP_ #define MYCLASSHPP_ namespace A
Consider the following C#: // C# .net switch(x) { case 1: for(int i =

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.