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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:00:50+00:00 2026-06-13T22:00:50+00:00

I have a numpy matrix and would like to concatenate all of the rows

  • 0

I have a numpy matrix and would like to concatenate all of the rows together so I end up with one long array.

#example

input:
[[1 2 3]
 [4 5 6}
 [7 8 9]]

output:
[[1 2 3 4 5 6 7 8 9]]

The way I am doing it now doe not seem pythonic. I’m sure there is a better way.

combined_x = x[0] 
for index, row in enumerate(x):
    if index!= 0:
        combined_x = np.concatenate((combined_x,x[index]),axis=1)

Thank you for the help.

  • 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-13T22:00:51+00:00Added an answer on June 13, 2026 at 10:00 pm

    I would suggest the ravel or flatten method of ndarray.

    >>> a = numpy.arange(9).reshape(3, 3)
    >>> a.ravel()
    array([0, 1, 2, 3, 4, 5, 6, 7, 8])
    

    ravel is faster than concatenate and flatten because it doesn’t return a copy unless it has to:

    >>> a.ravel()[5] = 99
    >>> a
    array([[ 0,  1,  2],
           [ 3,  4, 99],
           [ 6,  7,  8]])
    >>> a.flatten()[5] = 77
    >>> a
    array([[ 0,  1,  2],
           [ 3,  4, 99],
           [ 6,  7,  8]])
    

    But if you need a copy to avoid the memory sharing illustrated above, you’re better off using flatten than concatenate, as you can see from these timings:

    >>> %timeit a.ravel()
    1000000 loops, best of 3: 468 ns per loop
    >>> %timeit a.flatten()
    1000000 loops, best of 3: 1.42 us per loop
    >>> %timeit numpy.concatenate(a)
    100000 loops, best of 3: 2.26 us per loop
    

    Note also that you can achieve the exact result that your output illustrates (a one-row 2-d array) with reshape (thanks Pierre GM!):

    >>> a = numpy.arange(9).reshape(3, 3)
    >>> a.reshape(1, -1)
    array([[0, 1, 2, 3, 4, 5, 6, 7, 8]])
    >>> %timeit a.reshape(1, -1)
    1000000 loops, best of 3: 736 ns per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Numpy and have a 7x12x12 matrix whose values I would like to
I have a matrix in the type of a Numpy array. How would I
I have an NxM array in numpy that I would like to take the
I am using numpy. I have one array Y and one matrix X .
I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a Numpy array that looks like >>> a array([[ 3. , 2.
I have some data represented in a 1300x1341 matrix. I would like to split
If I have the following matrix: import numpy ar = numpy.array((('0','1','2','3'), ('1','a','b','b'), ('2','b','c','d')), str)
Say I have a numpy matrix like so: [[ x1, x2, x3, ... ],

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.