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

  • Home
  • SEARCH
  • 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 9218195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:48:21+00:00 2026-06-18T02:48:21+00:00

This works in MATLAB: >> p = [1, 0, 2, 4, 3, 6, 5];

  • 0

This works in MATLAB:

>> p = [1, 0, 2, 4, 3, 6, 5];
>> p(p+1)

ans = 

     0   1   2   3   4   5   6

Is there a way to do the same thing in NumPy? I can’t figure out how:

>>> p = mat([1, 0, 2, 4, 3, 6, 5])
>>> p[p]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 305, in __getitem__
    out = N.ndarray.__getitem__(self, index)
IndexError: index (1) out of range (0<=index<0) in dimension 0
>>> p[:,p]

The interpreter seems to go into an infinite loop at this point. This also causes an infinite loop:

>>> [p[:,i] for i in p]

But this works:

>>> [p[:,i] for in range(0,6)]

So it is something about using a matrix member as its own indices that causes the problem. Is this a bug in Python? Or am I doing something wrong?

  • 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-18T02:48:22+00:00Added an answer on June 18, 2026 at 2:48 am

    Only integers can be used as array or matrix indices. The default type for a matrix initialised like that is float.

    You can use a numpy.array not a numpy.matrix:

    In [2]: import numpy as np
    In [3]: x = np.array([1, 0, 2, 4, 3, 6, 5])
    In [4]: x[x]
    Out[4]: array([0, 1, 2, 3, 4, 5, 6])
    

    Or you can explicitly change your matrix to an integer type:

    In [5]: x = np.matrix(x).astype(int)
    In [6]: x[0, x]
    Out[7]: matrix([[0, 1, 2, 3, 4, 5, 6]])
    

    A numpy.matrix is a specialised class designed for 2D matrices. In particular, you can’t index a 2D matrix with a single integer, because — well — it’s two dimensional and you need to specify two integers, hence the need for the extra 0 index in the second example.

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

Sidebar

Related Questions

When I run this code: >> I = imread('D:\Works\matlab\SecCode.php.png','png'); >> imshow(I); It always shows
This works when I have only one state code as a parameter. How can
In both MATLAB and Numpy, arrays can be indexed by arrays. However, the behavior
This works. $(document).ready(function(){ $(.items article).click(function(){ window.location=$(this).find(a).attr(href); return false; }); }); However , when the
This works but for some reason the comma is missing. data gets inserted as
This works: <cfdirectory action=list name=qry directory=C:\inetpub\wwwroot\Atlas\ > </cfdirectory> <cfdump var=#qry#> So I know that
This works in chromium (and I assume every other major browser) -html- <invalid></invalid> -css-
This works: require 'csv' file = CSV.open(filename) puts file.shift This does not: require 'csv'
This works when I used a rectangle as the food but now I'm trying
This works, but when I hover the first time, nothing loads. When I mouse

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.