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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:18:56+00:00 2026-06-05T11:18:56+00:00

I have a NumPy matrix which I’ve simplified to exemplify: a b c d

  • 0

I have a NumPy matrix which I’ve simplified to exemplify:

       a  b  c  d  e  f 
A =  [[0, 1, 2, 3, 4, 5],
 b    [1, 0, 3, 4, 5, 6],
 c    [2, 3, 0, 5, 6, 7],
 d    [3, 4, 5, 0, 7, 8],
 e    [4, 5, 6, 7, 0, 9],
 f    [5, 6, 7, 8, 9, 0]]

where the number at the “intersections” is important, but their order is not right. I want to re-arrange the rows and columns such that the new order is [a, d, b, e, c, f] but this value that I’m calling “the intersection” is the same.

Below I have started to transform the matrix how I want. Filling the ‘e’ row involves looking at the intersections above for (e,a) (= 4), then (e,d) (=7) , then (e,b) (=5), (e,e), (e,c), and (e,f)

       a  d  b  e  c  f
A1=  [[0, 3, 1, 4, 2, 5],
 d    [3, 0, 4, 7, 5, 8],
 b    [1, 4, 0, 5, 3, 6],  
 e    [4, 7, 5, 

Can anyone please suggest how to re-arrange my matrix in this manner?

  • 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-05T11:18:57+00:00Added an answer on June 5, 2026 at 11:18 am

    edit: I just stumbled across a NumPy solution that uses advanced indexing:

    #                 a  b  c  d  e  f
    A = numpy.array([[0, 1, 2, 3, 4, 5],
                     [1, 0, 3, 4, 5, 6],
                     [2, 3, 0, 5, 6, 7],
                     [3, 4, 5, 0, 7, 8],
                     [4, 5, 6, 7, 0, 9],
                     [5, 6, 7, 8, 9, 0]])
    
    #            a  d  b  e  c  f
    new_order = [0, 3, 1, 4, 2, 5]
    A1 = A[:, new_order][new_order]
    

    Here is a pure Python solution which may be transferable to NumPy:

    #     a  b  c  d  e  f
    A = [[0, 1, 2, 3, 4, 5],
         [1, 0, 3, 4, 5, 6],
         [2, 3, 0, 5, 6, 7],
         [3, 4, 5, 0, 7, 8],
         [4, 5, 6, 7, 0, 9],
         [5, 6, 7, 8, 9, 0]]
    
    #            a  d  b  e  c  f
    new_order = [0, 3, 1, 4, 2, 5]    # maps previous index to new index
    A1 = [[A[i][j] for j in new_order] for i in new_order]
    

    Result:

    >>> pprint.pprint(A1)
    [[0, 3, 1, 4, 2, 5],
     [3, 0, 4, 7, 5, 8],
     [1, 4, 0, 5, 3, 6],
     [4, 7, 5, 0, 6, 9],
     [2, 5, 3, 6, 0, 7],
     [5, 8, 6, 9, 7, 0]]
    

    Here is a version that modifies A in place:

    A[:] = [A[i] for i in new_order]
    for row in A:
        row[:] = [row[i] for i in new_order]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following numpy matrix that I want to sort in ascending order
I have a 2D numpy array (i.e matrix) A which contains useful data interspread
In short: I have two matrices (or arrays): import numpy block_1 = numpy.matrix([[ 0,
I have a matrix in the type of a Numpy array. How would I
Say I have a numpy matrix like so: [[ x1, x2, x3, ... ],
I am using numpy. I have a matrix with 1 column and N rows
Let's suppose I have a numpy matrix variable called MATRIX with 3 coordinates: (x,
I have a number of spheres in 3d space which the user should be
If I have the following matrix: import numpy ar = numpy.array((('0','1','2','3'), ('1','a','b','b'), ('2','b','c','d')), str)
I have a NxN matrix which I want to split into non-overlap KxK block.

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.