Computer Science, asked by janvisharma11220, 5 months ago

let there be two numpy arrays,n1and n2
n1=[45,60]
n2=[55,40]​

Answers

Answered by mohamedshaheedh2712
18

Explanation:

I have a numpy array, and for the sake of argument, let it be defined as follows:

import numpy as np

a = np.arange(100)

a.shape = (10,10)

# 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, 25, 26, 27, 28, 29],

# [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],

# [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],

# [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],

# [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],

# [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],

# [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],

# [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

now I want to choose rows and columns of a specified by vectors n1 and n2. As an example:

n1 = range(5)

n2 = range(5)

But when I use:

b = a[n1,n2]

# array([ 0, 11, 22, 33, 44])

Then only the first fifth diagonal elements are chosen, not the whole 5x5 block. The solution I have found is to do it like this:

b = a[n1,:]

b = b[:,n2]

# array([[ 0, 1, 2, 3, 4],

# [10, 11, 12, 13, 14],

# [20, 21, 22, 23, 24],

# [30, 31, 32, 33, 34],

# [40, 41, 42, 43, 44]])

But I am sure there should be a way to do this simple task in just one command.

Answered by shivrajkumbhar1302
1

Answer:

9875dghj hgfhk hggkahh vabBwjbsnj sbn

Similar questions