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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:17:12+00:00 2026-05-23T17:17:12+00:00

Im working with two arrays, trying to work with them like a 2 dimensional

  • 0

Im working with two arrays, trying to work with them like a 2 dimensional array. I’m using a lot of vectorized calculations with NumPy. Any idea how I would populate an array like this:

X = [1, 2, 3, 1, 2, 3, 1, 2, 3]

or:

X = [0.2, 0.4, 0.6, 0.8, 0.2, 0.4, 0.6, 0.8, 0.2, 0.4, 0.6, 0.8, 0.2, 0.4, 0.6, 0.8]

Ignore the first part of the message.

I had to populate two arrays in a form of a grid. But the grid dimensions varied from the users, thats why I needed a general form. I worked on it all this morning and finally got what I wanted.

I apologize if I caused any confusion earlier. English is not my tongue language, and sometimes it is hard for me to explain things.

This is the code that did the job for me:

    myIter = linspace(1, N, N)
    for x in myIter:
        for y in myIter:
            index = ((x - 1)*N + y) - 1
            X[index] = x / (N+1)
            Y[index] = y / (N+1)

The user inputs N.
And the length of X, Y is N*N.

  • 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-23T17:17:13+00:00Added an answer on May 23, 2026 at 5:17 pm

    You can use the function tile. From the examples:

    >>> a = np.array([0, 1, 2])
    >>> np.tile(a, 2)
    array([0, 1, 2, 0, 1, 2])
    

    With this function, you can also reshape your array at once like they do in the other answers with reshape (by defining the ‘repeats’ is more dimensions):

    >>> np.tile(a, (2, 1))
    array([[0, 1, 2],
           [0, 1, 2]])
    

    Addition: and a little comparison of the difference in speed between the built in function tile and the multiplication:

    In [3]: %timeit numpy.array([1, 2, 3]* 3)
    100000 loops, best of 3: 16.3 us per loop
    In [4]: %timeit numpy.tile(numpy.array([1, 2, 3]), 3)
    10000 loops, best of 3: 37 us per loop
    
    In [5]: %timeit numpy.array([1, 2, 3]* 1000)
    1000 loops, best of 3: 1.85 ms per loop
    In [6]: %timeit numpy.tile(numpy.array([1, 2, 3]), 1000)
    10000 loops, best of 3: 122 us per loop 
    

    EDIT

    The output of the code you gave in your question can also be achieved as following:

    arr = myIter / (N + 1)
    X = numpy.repeat(arr, N)
    Y = numpy.tile(arr, N)
    

    This way you can avoid looping the arrays (which is one of the great advantages of using numpy). The resulting code is simpler (if you know the functions of course, see the documentation for repeat and tile) and faster.

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

Sidebar

Related Questions

Why when working with two dimensional arrays only second dimension is important for a
I'm trying to convert a two-dimensional array into a structured array with named fields.
I am trying to use array_combine to combine two multi-dimensional arrays, but somehow not
I am currently working on two projects, and I am using Fogbugz to keep
I'm working with two independent c/c++ applications on Windows where one of them constantly
I'm working on mapping two objects in .NET. I would like to be able
The company I used to work with has two developers working fulltime, and a
I would like to separate the API I'm working on into two sections: 'bare-bones'
Trying to sort an array by writing my own sort method using recursion (Pine's
I'm working with 3 different arrays (although I'm only testing with two for the

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.