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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:40:54+00:00 2026-05-17T19:40:54+00:00

I have a matrix A which is: A <- matrix(c(1:15), byrow=T, nrow=5) A [,1]

  • 0

I have a matrix A which is:

A <- matrix(c(1:15), byrow=T, nrow=5)
A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
[5,]   13   14   15

Now I want to create a matrix B, which is 8×8 dimensions (or 10×10, or 15×15, etc), which would look like this:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    0    0    0    0    0
[2,]    4    5    6    0    0    0    0    0
[3,]    7    8    9    0    0    0    0    0
[4,]   10   11   12    0    0    0    0    0
[5,]   13   14   15    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

So starting from A, I want to add columns and rows, to an 8×8 dimensions, all replaced with zero values…
Any idea?
Thank you in advance!!

  • 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-17T19:40:55+00:00Added an answer on May 17, 2026 at 7:40 pm

    Try this assuming A has at least one row and one column:

    B <- matrix(0, 8, 8)
    B[1:nrow(A), 1:ncol(A)] <- A
    

    or as a single statement:

    B <- "[<-"(matrix(0, 8, 8), 1:nrow(A), 1:ncol(A), value = A)
    

    If A can have zero rows or zero columns then use seq_len(nrow(A)) and seq_len(ncol(A)) in place of 1:nrow(A) and 1:ncol(A).

    Alternately, this works even in the case that A has zero rows or columns:

    B <- matrix(0, 8, 8)
    B[cbind(c(row(A)), c(col(A)))] <- A
    

    or

    B <- "[<-"(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), value = A)
    

    or

    B <- replace(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), A)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix and i want to create a new matrix which will
I have a large matrix from which I would like to gather a collection
I have a set of data in a matrix which I would like to
I have a matrix which is 36 x 2, but I want to seperate
I want to write a function to create an empty square matrix have size
Say I have the following matrix which is vertical : {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} And I want
I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to
I have a NxN matrix which I want to split into non-overlap KxK block.
I have a large matrix from which I would like to randomly extract a
I have two dimensional matrix which stores values between 0 to 1. I want

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.