Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2-D arrays with numpy arange

so I'm trying to construct 2-D arrays with the numpy function arange and I'm having a bit of trouble.

I'd like to construct a 2D array of ints where the entry at position i,j is (i+j). That is, an array like this (reccommended to use arange):

[[ 0  1  2  3  4  5  6  7  8  9]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 2  3  4  5  6  7  8  9 10 11]
 [ 3  4  5  6  7  8  9 10 11 12]
 [ 4  5  6  7  8  9 10 11 12 13]
 [ 5  6  7  8  9 10 11 12 13 14]
 [ 6  7  8  9 10 11 12 13 14 15]
 [ 7  8  9 10 11 12 13 14 15 16]
 [ 8  9 10 11 12 13 14 15 16 17]
 [ 9 10 11 12 13 14 15 16 17 18]]

I also need to construct another array (100x100) where the value at index i,j is True if j is a divisor of i and False otherwise. That is, an array that looks like:

[[False False False ..., False False False]
 [ True  True  True ...,  True  True  True]
 [ True False  True ..., False  True False]
 ..., 
 [ True False False ...,  True False False]
 [ True False False ..., False  True False]
 [ True False False ..., False False  True]]

I am not able to use nested loops (though I can use loops to construct the lists) and I cannot use the np.array function. I currently have the following that works for the first part, but I would like to have it all as one array, not several printed out.

i = 0
j= 10
for i in range(10):
    lis = np.arange(i, j)
    i += 1
    j += 1
    print(np.array(lis))

If I could get some help, that'd be great

EDIT: my current code shows this output:

[0 1 2 3 4 5 6 7 8 9]
[ 1  2  3  4  5  6  7  8  9 10]
[ 2  3  4  5  6  7  8  9 10 11]
[ 3  4  5  6  7  8  9 10 11 12]
[ 4  5  6  7  8  9 10 11 12 13]
[ 5  6  7  8  9 10 11 12 13 14]
[ 6  7  8  9 10 11 12 13 14 15]
[ 7  8  9 10 11 12 13 14 15 16]
[ 8  9 10 11 12 13 14 15 16 17]
[ 9 10 11 12 13 14 15 16 17 18]

Why would the first line not be lining up with the other rows?

like image 517
schCivil Avatar asked Dec 23 '22 23:12

schCivil


2 Answers

To do the first one with numpy:

>>> a = np.arange(11)
>>> a[:,None]+a
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10],  
  [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],  
  [ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12],  
  [ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13],  
  [ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14],  
  [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],  
  [ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16],  
  [ 7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17],  
  [ 8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18],  
  [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],  
  [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])  

For the second array, @Divakar has a good approach. Maybe a bit simpler syntax to do this:

>>> (a%a[:,None])==0
array([[ True,  True,  True,  True,  True,  True,  True,  True,  True, True,  True],
   [ True,  True,  True,  True,  True,  True,  True,  True,  True, True,  True],
   [ True, False,  True, False,  True, False,  True, False,  True, False,  True],
   [ True, False, False,  True, False, False,  True, False, False, True, False],
   [ True, False, False, False,  True, False, False, False,  True, False, False],
   [ True, False, False, False, False,  True, False, False, False, False,  True],
   [ True, False, False, False, False, False,  True, False, False, False, False],
   [ True, False, False, False, False, False, False,  True, False, False, False],
   [ True, False, False, False, False, False, False, False,  True, False, False],
   [ True, False, False, False, False, False, False, False, False, True, False],
   [ True, False, False, False, False, False, False, False, False, False,  True]], dtype=bool)
like image 141
Dr Xorile Avatar answered Jan 04 '23 23:01

Dr Xorile


Per your first question:

np.add(*np.indices((nrow, ncol)))

For nrow=5, ncol=6 you get

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

This method doesn't use the numpy.arange function, though I find it more readable. Moreover, it supports cases when nrow != ncol.

like image 35
Eli Korvigo Avatar answered Jan 05 '23 00:01

Eli Korvigo