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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:35:49+00:00 2026-05-16T18:35:49+00:00

How is the sort() working in matlab? Code in pure matlab: q is an

  • 0

How is the sort() working in matlab?
Code in pure matlab:
q is an array:

q = -0.2461    2.9531  -15.8867   49.8750  -99.1172  125.8438  -99.1172   
49.8750  -15.8867    2.9531   -0.2461


After q = sort(roots(q)), I got:

q = 0.3525
0.3371 - 0.1564i
0.3371 + 0.1564i
0.2694 - 0.3547i
0.2694 + 0.3547i
1.3579 - 1.7880i
1.3579 + 1.7880i
2.4410 - 1.1324i
2.4410 + 1.1324i
2.8365


Well, seems to work fine! Then in python, I use (q is the same as above, it is an np.array):

import numpy as np
q = np.sort(np.roots(q))

And I got:

[ 0.26937874-0.35469815j  0.26937874+0.35469815j  0.33711562-0.15638427j
 0.33711562+0.15638427j  0.35254298+0.j          1.35792218-1.78801226j
 1.35792218+1.78801226j  2.44104520-1.13237431j  2.44104520+1.13237431j
 2.83653354+0.j        ]

Well… These two results seem different in that they sort differently, so what are the reasons? did I make something wrong? thank you in advance!

My answer:

def sortComplex(complexList):
    complexList.sort(key=abs)
    # then sort by the angles, swap those in descending orders
    return complexList   


Then call it in the python code, works fine :p

  • 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-16T18:35:50+00:00Added an answer on May 16, 2026 at 6:35 pm

    From the MATLAB documentation for SORT:

    If A has complex entries r and s,
    sort orders them according to the
    following rule: r appears before s in
    sort(A) if either of the following
    hold:

    • abs(r) < abs(s)
    • abs(r) = abs(s) and angle(r) < angle(s)

    In other words, an array that has complex entries is first sorted based on the absolute value (i.e. complex magnitude) of those entries, and any entries that have the same absolute value are sorted based on their phase angles.

    Python (i.e. numpy) orders things differently. From the documentation Amro linked to in his comment:

    The sort order for complex numbers is
    lexicographic. If both the real and
    imaginary parts are non-nan then the
    order is determined by the real parts
    except when they are equal, in which
    case the order is determined by the
    imaginary parts.

    In other words, an array that has complex entries is first sorted based on the real component of the entries, and any entries that have equal real components are sorted based on their imaginary components.

    EDIT:

    If you want to reproduce the numpy behavior in MATLAB, one way you can do it is to use the function SORTROWS to create a sort index based on the real and imaginary components of the array entries, then apply that sort index to your array of complex values:

    >> r = roots(q);  %# Compute your roots
    >> [junk,index] = sortrows([real(r) imag(r)],[1 2]);  %# Sort based on real,
                                                          %#   then imaginary parts
    >> r = r(index)  %# Apply the sort index to r
    
    r =
    
       0.2694 - 0.3547i
       0.2694 + 0.3547i
       0.3369 - 0.1564i
       0.3369 + 0.1564i
       0.3528          
       1.3579 - 1.7879i
       1.3579 + 1.7879i
       2.4419 - 1.1332i
       2.4419 + 1.1332i
       2.8344          
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.