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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:55:06+00:00 2026-05-22T16:55:06+00:00

I have an 2d array, A that is 6×6. I would like to take

  • 0

I have an 2d array, A that is 6×6. I would like to take the first 2 values (index 0,0 and 0,1) and take the average of the two and insert the average into a new array that is half the column size of A (6×3) at index 0,0. Then i would get the next two indexes at A, take average and put into the new array at 0,1.

The only way I know how to do this is using a double for loop, but for performance purposes (I will be using arrays as big as 3000×3000) I know there is a better solution out there! Thanks!

  • 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-22T16:55:07+00:00Added an answer on May 22, 2026 at 4:55 pm

    A very useful feature of numpy arrays is that they can be reshaped and viewed in many different ways, and by doing so, you can make certain operations very easy.

    Since you want to pair every two items, it makes sense to reshape the 6×6 array into a 18×2 array:

    import numpy as np
    
    arr=np.arange(36).reshape(6,6)
    print(arr)
    # [[ 0  1  2  3  4  5]
    #  [ 6  7  8  9 10 11]
    #  [12 13 14 15 16 17]
    #  [18 19 20 21 22 23]
    #  [24 25 26 27 28 29]
    #  [30 31 32 33 34 35]]
    arr2=arr.reshape(-1,2)
    print(arr2)
    # [[ 0  1]
    #  [ 2  3]
    #  [ 4  5]
    #  [ 6  7]
    #  [ 8  9]
    #  [10 11]
    #  [12 13]
    #  [14 15]
    #  [16 17]
    #  [18 19]
    #  [20 21]
    #  [22 23]
    #  [24 25]
    #  [26 27]
    #  [28 29]
    #  [30 31]
    #  [32 33]
    #  [34 35]]
    

    Now taking the average is easy:

    means=arr2.mean(axis=1)
    print(means)
    # [  0.5   2.5   4.5   6.5   8.5  10.5  12.5  14.5  16.5  18.5  20.5  22.5
    #   24.5  26.5  28.5  30.5  32.5  34.5]
    

    And finally, we just reshape the array to be 6×3:

    means=means.reshape(6,-1)
    print(means)
    # [[  0.5   2.5   4.5]
    #  [  6.5   8.5  10.5]
    #  [ 12.5  14.5  16.5]
    #  [ 18.5  20.5  22.5]
    #  [ 24.5  26.5  28.5]
    #  [ 30.5  32.5  34.5]]
    

    or, as a 1-liner:

    means=arr.reshape(-1,2).mean(axis=1).reshape(6,-1)
    

    PS: reshaping is a very quick operation, since it is returning a view, not a copy of the original array. All that is changed is the dimensions and the strides. All that remains is one call to the mean method. Thus, this solution should be about a quick as possible using numpy.

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

Sidebar

Related Questions

I have an array that looks like $numbers = array('first', 'second', 'third'); I want
I have an array that I would like to sort, that should be easy,
I have a two dimensional array that I need to load data into. I
I have an array that contains several arrays and I would like to order
I have an array that looks like: $fields = array( f1 => array(test), f2
I have an array that has keys and values. For eg: Array ( [name]
I have a array that looks like this Array ( [provider] => Array (
I have an array that contains the RGB colour values for each pixel in
I have an array that looks like this: Array ( [0] => Array (
I have an array that looks like this: ( { title = Chris; isMale

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.