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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:40:52+00:00 2026-06-07T09:40:52+00:00

In both MATLAB and Numpy, arrays can be indexed by arrays. However, the behavior

  • 0

In both MATLAB and Numpy, arrays can be indexed by arrays. However, the behavior is different. Let me explain this by an example.

MATLAB:

>> A = rand(5,5)

A =

    0.1622    0.6020    0.4505    0.8258    0.1067
    0.7943    0.2630    0.0838    0.5383    0.9619
    0.3112    0.6541    0.2290    0.9961    0.0046
    0.5285    0.6892    0.9133    0.0782    0.7749
    0.1656    0.7482    0.1524    0.4427    0.8173

>> A([1,3,5],[1,3,5])

ans =

    0.1622    0.4505    0.1067
    0.3112    0.2290    0.0046
    0.1656    0.1524    0.8173

Numpy:

In [2]: A = arange(25).reshape((5,5))

In [3]: A
Out[3]: 
array([[ 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]])

In [6]: A[[0,2,4], [0,2,4]]
Out[6]: array([ 0, 12, 24])

In words: MATLAB selects rows and columns, Numpy “zips” the two index arrays and uses the tuples to point to entries.

How can I get the MATLAB behavior with Numpy?

  • 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-07T09:40:54+00:00Added an answer on June 7, 2026 at 9:40 am

    You can do this:

    A[[0,2,4],:][:,[0,2,4]]
    

    which will give the MATLAB-like result you want.

    It’s worth being aware that, rather inconsistently, if you use slices for indexing then you get MATLAB-like results without any such hackery:

    >>> A[1:3,1:3]
    array([[ 6, 7],
           [11,12]])
    

    In numpy, unlike MATLAB, 1:3 is not just an abbreviation for [1,2] or anything of the kind. (At which point I feel obliged to mention something you surely know already, namely that Python’s 1:3 is kinda like [1,2] whereas MATLAB’s is kinda like [1,2,3]: the right-hand endpoint is included in MATLAB and excluded in Python.)

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

Sidebar

Related Questions

I don't know if Matlab can do this, but I want to store some
Both can be used for communicating between different processes, what's the difference?
Both are BDD (Behavior Driven Development) capable unit test frameworks for Scala written in
I've been working with HDF5 files with C and Matlab , both using the
I am wondering whether I can do the following efficiently in Matlab. Writing a
Doing a couple rotations in Matlab, one which is rotation about the y-axis, however
I just started matlab and need to finish this program really fast, so I
Does anyone know of a Python replacement for Matlab / Octave bwdist() function? This
I'm quite new to matlab, but I know how to do both for loops
In Matlab, by the function min(), I can only get one single minimum element

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.