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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:15:57+00:00 2026-05-21T16:15:57+00:00

I have a matrix with diagonals equal to zero and off-diagonals all equal to

  • 0

I have a matrix with diagonals equal to zero and off-diagonals all equal to one (the inverse of an identity matrix):

mat1 <- matrix(c(0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0), 5, 5)

I also have a vector that is always the same length as the dims of the matrix and always starts at zero:

vec1 <- c(0,1,2,3,4)

using these two objects I want to create a matrix that looks like this:

mat2 <- matrix(c(0,1,2,3,4,1,0,1,2,3,2,1,0,1,2,3,2,1,0,1,4,3,2,1,0), 5, 5)

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

I want an operation that will generalize so that if I have a matrix of dims 9 by 9, for example, and a vector of 0:8 I can achieve the equivalent result. Any ideas for how to approach this?

  • 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-21T16:15:58+00:00Added an answer on May 21, 2026 at 4:15 pm

    As vec1 starts with a zero, then you can do :

    MakeMatrix <- function(x){
      n <- length(x)
      id <- abs(rep(1:n,n)-rep(1:n,each=n)) + 1
      matrix(x[id],ncol=n)
    }
    
    MakeMatrix(vec1)
    

    So there’s no need to take the mat1 in the input, as that one is actually redundant. You can just construct the matrix within the function.

    The trick is in providing a sequence of id values to select from the vector, and then transform everything to a matrix.


    Edit : If you’re only going to use sequences, you could as well do :

    MakeMatrix <- function(n){
      id <- abs(rep(1:n,n)-rep(1:n,each=n))
      matrix(id,ncol=n)
    }
    
    MakeMatrix(7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an STL vector that stores the diagonals of a matrix. A certain
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix of objects that contains data in this form: name A,2,name
I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat
I have a matrix A with size (nr,nc), a vector of column indices B
I have 2 matrix structs means equal data but have different form like these:
I have a matrix class that takes the row and column reference type that
I have like a matrix in R and I want to get: Max off
I have 8x8 matrix where I have 15 diagonals. upper left corner diagonal 1
I have a diagonal matrix with values -3.4562e-14, 15.2673, 86.9550, 1.4873e+03. Matlab says that

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.