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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:25:50+00:00 2026-06-14T04:25:50+00:00

in matlab/ GNU Octave( which i am actually using ), I use this method

  • 0

in matlab/ GNU Octave( which i am actually using ), I use this method to copy particular elements of a 2D array to another 2D array:

B(2:6, 2:6) = A

where

size(A) = (5, 5)

My question is, “How can this be achieved in python using numpy?”
currently, for example, I am using the following nested loop in python:

>>> import numpy as np
>>> a = np.int32(np.random.rand(5,5)*10)
>>> b = np.zeros((6,6), dtype = np.int32)

>>> print a
[[6 7 5 1 3]
 [3 9 7 2 0]
 [9 3 7 6 7]
 [9 8 2 0 8]
 [8 7 7 9 9]]
>>> print b
[[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, 0, 0],
 [0, 0, 0, 0, 0, 0]]

>>> for i in range(1,6):
        for j in range(1,6):
            b[i][j] = a[i-1][j-1]
>>> print b
[[0, 0, 0, 0, 0, 0],
 [0, 6, 7, 5, 1, 3],
 [0, 3, 9, 7, 2, 0],
 [0, 9, 3, 7, 6, 7],
 [0, 9, 8, 2, 0, 8],
 [0, 8, 7, 7, 9, 9]]

Is there a better way to do 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-06-14T04:25:52+00:00Added an answer on June 14, 2026 at 4:25 am

    It’s almost the same as the MATLAB:

    b[1:6, 1:6] = a
    

    The only thing is that Python uses 0-based indexing so the second element is 1 instead of 2.

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

Sidebar

Related Questions

In Matlab or GNU Octave I'd like to do something like this: x=struct('a',1,'b',2,'c',[1 2;3
I'm using the cov (covariance) function of Matlab and Octave. Actually I'm using Octave
version: Matlab 2009a I am generating a vector of size <1x116286> using randsrc() function.
In MATLAB, I have data which looks like this: 5 7 1 5 8
in MATLAB I have a cell array like this a = { 1 2
My matlab code does image processing, and I made matlab function which has 2
In MATLAB I'm using a couple of java routines I've written to interface with
When Matlab is installed in its 64 bit version, it can use only 64
Matlab's cross-correlation function xcorr(x,y,maxlags) has an option maxlag , which returns the cross-correlation sequence
In MATLAB, I have a for loop which has a lot of interations to

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.