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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:39:13+00:00 2026-06-10T06:39:13+00:00

Let me explain it by a small example: >>> x = np.array([[1,2], [3,4], [5,6],

  • 0

Let me explain it by a small example:

>>> x = np.array([[1,2], [3,4], [5,6], [7,8]])
>>> x
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

I want to have a new array that has the form

array([[0, 0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7, 8],
       [5, 6, 7, 8, 0, 0]])

Here, the context has the size +/-1, but I’d like to keep it variable.

What I’m doing so far is appending zeros to the original array:

>>> y = np.concatenate((np.zeros((1, 2)), x, np.zeros((1, 2))), axis=0)
>>> y
array([[ 0.,  0.],
       [ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.],
       [ 7.,  8.],
       [ 0.,  0.]])

And putting the values into a new array by reading rows of the new size:

>>> z = np.empty((x.shape[0], x.shape[1]*3))
>>> for i in range(x.shape[0]): z[i] = y[i:i+3].flatten()

That kind of works, but I find it slow, ugly and unpythonic.
Can you think of a better way to do this rearrangement? Additional thumbsup for an in-place-ish solution 🙂

  • 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-10T06:39:15+00:00Added an answer on June 10, 2026 at 6:39 am

    There is the option of using stride_tricks, but I will not say that this is the best answer, because while it is “the most efficient way”, that way is not always the best when considering readability and that it is playing with fire.

    # We make it flat (and copy if necessary) to be on the safe side, and because
    # it is more obvious this way with stride tricks (or my function below):
    y = y.ravel()
    
    # the new shape is (y.shape[0]//2-2, 6). When looking at the raveled y, the first
    # dimension takes steps of 2 elements (so y.strides[0]*2) and the second is
    # just the old one:
    z = np.lib.stride_tricks.as_strided(y, shape=(y.shape[0]//2-2, 6),
                                           strides=(y.strides[0]*2, y.strides[0]))
    

    Note that z here is only a view, so use z.copy() to avoid any unexpected things before editing it, otherwise in your example all 1s will change if you edit one of them. On the up side, if you mean this by “in-place”, you can now change elements in y and z will change too.

    If you want to do more of this magic, maybe check out my rolling_window function from https://gist.github.com/3430219, which replaces the last line with:

    # 6 values long window, but only every 2nd step on the original array:
    z = rolling_window(y, 6, asteps=2)
    

    Important: np.lib.stride_tricks.as_strided by itself is generally not safe and must be used with care as it can create segmentation faults.

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

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
First let me explain. I have several addresses on the page that I put
Quite new to maven here so let me explain first what I am trying
OK, this is impossible, but I will try to explain the situation here. Let's
Let me explain what I mean in my title. Let's say, that for example
Let me explain with an example. If n=4 and r=2 that means all 4
Let me explain with an example. We have the following text: Comme Il Faut
Let me explain you my situation. I have a base class called Shape, and
let me explain my current situation i have a SharePoint site lets say it
Let me explain. Suppose I want to teach Python to someone who only speaks

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.