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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:22:42+00:00 2026-05-26T04:22:42+00:00

I have a 2D numpy array and I have a arrays of rows and

  • 0

I have a 2D numpy array and I have a arrays of rows and columns which should be set to a particular value. Lets consider the following example

 a = array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])

I want to modify entries at rows [0,2] and columns [1,2]. This should result in the following array

 a = array([[1, 2, 0],
           [4, 5, 0],
           [7, 8, 9]])

I did following and it resulted in modifying each sequence of column in every row

rows = [0,1]
cols = [2,2]
b=a[numpy.ix_(rows,columns)]

It resulted in the following array modifying every column of the specified array

array([[1, 0, 0],
       [4, 5, 6],
       [7, 0, 0]])

Some one could please let me know how to do it?

Thanks a lot

EDIT: It is to be noted that rows and columns coincidently happend to be sequentia. The actual point is that these could be arbitrary and in any order. if it is rows = [a,b,c] and cols=[n x z] then I want to modify exactly three elements at locations (a,n),(b,x),(c,z).

  • 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-26T04:22:43+00:00Added an answer on May 26, 2026 at 4:22 am

    Adding to what others have said, you can modify these elements using fancy indexing as follows:

    In [39]: rows = [0,1]
    
    In [40]: cols = [2,2]
    
    In [41]: a = np.arange(1,10).reshape((3,3))
    
    In [42]: a[rows,cols] = 0
    
    In [43]: a
    Out[43]: 
    array([[1, 2, 0],
           [4, 5, 0],
           [7, 8, 9]])
    

    You might want to read the documentation on indexing multidimensional arrays:
    http://docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing-multi-dimensional-arrays

    The key point is:

    if the index arrays have a matching shape, and there is an index array
    for each dimension of the array being indexed, the resultant array has
    the same shape as the index arrays, and the values correspond to the
    index set for each position in the index arrays.

    Importantly this also allows you to do things like:

    In [60]: a[rows,cols] = np.array([33,77])
    
    In [61]: a
    Out[61]: 
    array([[ 1,  2, 33],
           [ 4,  5, 77],
           [ 7,  8,  9]])
    

    where you can set each element independently using another array, list or tuple of the same size.

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

Sidebar

Related Questions

How does one add rows to a numpy array? I have an array A:
I have the following 3 x 3 x 3 numpy array called a (the
Consider the following convenient looping idiom. import numpy print shape of x = numpy.array([['a',
I'v got two numpy arrays. The first array contains some zeros (which are distributed
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 2-D numpy array with 100,000+ rows. I need to return a
I have a 2D numpy array (i.e matrix) A which contains useful data interspread
I have an array of doubles, roughly 200,000 rows by 100 columns, and I'm
I have a 2x2 numpy array : x = array(([[1,2],[4,5]])) which I must merge
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like

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.