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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:12:57+00:00 2026-05-28T07:12:57+00:00

What is equivalent pythonic implementation for the following simple piece of code in Matlab.

  • 0

What is equivalent pythonic implementation for the following simple piece of code in Matlab.

Matlab:


B = 2D array of integers as indices [1...100]
A = 2D array of numbers: [10x10]
A[B] = 0

which works well as for example for B[i]=42 it finds the position 2 of column 5 to be set.
In Python it causes an error: out of bound which is logical. However to translate the above Matlab code into Python we are looking for pythonic ways.
Please also consider the problem for higher dimensions such as:


B = 2D array of integers as indices [1...3000]
C = 3D array of numbers: [10x10x30]
C[B] = 0

One way we thought about it is to reform indices array elements as i,j instead of being absolute position. That is, position 42 to divmod(42,m=10)[::-1] >>> (2,4). So we will have a nx2 >>> ii,jj vectors of indices which can be used for indexing A easily.
We thought that it might be a better way, efficient also for higher dimensions in Python.

  • 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-28T07:12:57+00:00Added an answer on May 28, 2026 at 7:12 am

    You can use .ravel() on the array (A) before indexing it, and then .reshape() after.

    Alternatively, since you know A.shape, you can use np.unravel_index on the other array (B) before indexing.

    Example 1:

    >>> import numpy as np
    >>> A = np.ones((5,5), dtype=int)
    >>> B = [1, 3, 7, 23]
    >>> A
    array([[1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1]])
    >>> A_ = A.ravel()
    >>> A_[B] = 0
    >>> A_.reshape(A.shape)
    array([[1, 0, 1, 0, 1],
           [1, 1, 0, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 0, 1]])
    

    Example 2:

    >>> b_row, b_col = np.vstack([np.unravel_index(b, A.shape) for b in B]).T
    >>> A[b_row, b_col] = 0
    >>> A
    array([[1, 0, 1, 0, 1],
           [1, 1, 0, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 0, 1]])
    

    Discovered later: you can use numpy.put

    >>> import numpy as np
    >>> A = np.ones((5,5), dtype=int)
    >>> B = [1, 3, 7, 23]
    >>> A.put(B, [0]*len(B))
    >>> A
    array([[1, 0, 1, 0, 1],
           [1, 1, 0, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1],
           [1, 1, 1, 0, 1]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the NInject equivalent of the following code that uses Autofac: var builder
What is the equivalent Delphi code for the following in C: int32 *P; int32
Is there a pythonic way of getting the equivalent of the following in .NET?
What is the equivalent Linq to XML for the following code: public List<listing> GetList()
The following two expressions seem equivalent to me. Which one is preferable? data =
Equivalent (roughly) of setCurrent() in BlackBerry()? I have some J2ME code I am porting
What is JavaScript equivalent to Ruby's Array#compact? Long version.... I followed the examples at
Is there a Pythonic equivalent to Ruby's #each_cons ? In Ruby you can do
For the following construct what can the more pythonic way? If it were C
Equivalent of following methods in Joda-Time DateFormatSymbols #getMonths() /** * Gets month strings. For

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.