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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:10:54+00:00 2026-05-20T00:10:54+00:00

Suppose you have an array (m, m) and want to make it (n, n).

  • 0

Suppose you have an array (m, m) and want to make it (n, n). For example, transforming a 2×2 matrix to a 6×6. So:

[[ 1.  2.]
 [ 3.  4.]]

To:

[[ 1.  2.  0.  0.  0.  0.]
 [ 3.  4.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

This is what I’m doing:

def array_append(old_array, new_shape):
    old_shape = old_array.shape
    dif = np.array(new_shape) - np.array(old_array.shape)
    rows = []
    for i in xrange(dif[0]):
        rows.append(np.zeros((old_array.shape[0])).tolist())
    new_array = np.append(old_array, rows, axis=0)
    columns = []
    for i in xrange(len(new_array)):
        columns.append(np.zeros(dif[1]).tolist())
    return np.append(new_array, columns, axis=1)

Example use:

test1 = np.ones((2,2))
test2 = np.zeros((6,6))
print array_append(test1, test2.shape)

Output:

[[ 1.  1.  0.  0.  0.  0.]
 [ 1.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

Based on this answer. But that’s a lot of code for an (imho) simple operation. Is there a more concise/pythonic way to do it?

  • 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-20T00:10:54+00:00Added an answer on May 20, 2026 at 12:10 am

    Why not use array = numpy.zeros((6,6)), see the numpy docs…

    EDIT, woops, question has been edited… I guess you are trying to put ones in a section of an array filled with zeros? Then:

    array = numpy.zeros((6,6))
    array[0:2,0:2] = 1
    

    If the small matrix does not all have the value of 1:

    array[ystart:yend,xstart:xend] = smallermatrix
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem and I want to make sure if I am doing
Suppose I have an array String[] which has 47 characters in each element. The
Let's suppose I have the following line in a CSV file (I removed the
I have a Web Service written in Java. I want to send some strings
Suppose two lines of text correspond to each other word by word except for
OK I have so much questions regarding my file sharing application that I don't
I'm learning TDD, and I have a question about private / protected variables. My
In a Ruby on Rails project I need to have a JavaScript function executed
a[b].innerHTML=a[b].innerHTML.replace(RegExp('['+bbc[c][0]+']','ig'),bbc[c][1]) This is basically what I'm working with. It's wrapped in two loops so
I want to try to create a learning chess application as a school project.

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.