The objective of this code snippet was to create a 2D array of shape (10,10) with
array[0,0]=1;
array[0,9]=100; and
array[9,0]=50.
Complications arose when the interval between these elements had to be equal as shown in the expected output. Rows had to increment with equal intervals up-to 100 and columns had to increment with equal intervals up-to 50.
I know that my code has a logical error in list-comprehension for "matrix_list". But I'm not sure what the error is.
The code I wrote:
`import numpy as np`
`matrix_list = np.zeros((10,10), dtype = int)`
`matrix_list =
[(np.arange(column, 101, (100-1)/9).astype(int)) for column in np.arange(1, 51, (50-1)/9).astype(int)]`
`print(np.array(matrix_list))`
Expected Output:
[ 1, 12, 23, 34, 45, 56, 67, 78, 89, 100]
[ 6, 17, 28, 39, 50, 61, 72, 83, 94, 0]
[11, 22, 33, 44, 55, 66, 77, 88, 0, 0]
[17, 28, 39, 50, 61, 72, 83, 0, 0, 0]
[22, 33, 44, 55, 66, 77, 0, 0, 0, 0]
[28, 39, 50, 61, 72, 0, 0, 0, 0, 0]
[33, 44, 55, 66, 0, 0, 0, 0, 0, 0]
[39, 50, 61, 0, 0, 0, 0, 0, 0, 0]
[44, 55, 0, 0, 0, 0, 0, 0, 0, 0]
[50, 0, 0, 0, 0, 0, 0, 0, 0, 0]
The output I am getting:
[array([ 1, 12, 23, 34, 45, 56, 67, 78, 89, 100])
array([ 6, 17, 28, 39, 50, 61, 72, 83, 94])
array([11, 22, 33, 44, 55, 66, 77, 88, 99])
array([17, 28, 39, 50, 61, 72, 83, 94])
array([22, 33, 44, 55, 66, 77, 88, 99])
array([28, 39, 50, 61, 72, 83, 94]) array([33, 44, 55, 66, 77, 88, 99])
array([39, 50, 61, 72, 83, 94]) array([44, 55, 66, 77, 88, 99])
array([50, 61, 72, 83, 94])]
"""
CodePudding user response:
The main problem is that you're overwriting your pre-allocated array matrix_list with the result of the list comprehension, which is just a series of lists. Thus, you lose all of the structure that you defined to begin with. To make things simpler (since you also have an issue with making the numpy range up to the same amount each time, rather than decrementing sequentially like your desired output shows), see if you can get the zero-padding working on an individual array first, or even as part of a for loop.
If you want to create padded numpy arrays, you can use np.pad(X, pad_width=(before,after)), where the second argument allows you to specify how many padded values you will add before and after the array, X. The default behavior of the function is to add zeros wherever you want to pad, which is what you want.
As for getting everything to work in list comprehension, you can consider using enumerate(x) to help you figure out how many padding digits you'll need and where to stop your counting.
CodePudding user response:
The issue is that you create a numpy array and you immediately overwrite your variable with a list of list.
Anyway, you should handle this with numpy broadcasting and linspace
, then mask the lower right triangle with boolean indexing:
matrix = (
np.linspace(1, 100, 10, dtype=int)
np.linspace(0, 50-1, 10, dtype=int)[:,None]
)
n = np.arange(10)
matrix[(n >= (10-n)[:,None])] = 0
print(matrix)
Output:
[[ 1 12 23 34 45 56 67 78 89 100]
[ 6 17 28 39 50 61 72 83 94 0]
[ 11 22 33 44 55 66 77 88 0 0]
[ 17 28 39 50 61 72 83 0 0 0]
[ 22 33 44 55 66 77 0 0 0 0]
[ 28 39 50 61 72 0 0 0 0 0]
[ 33 44 55 66 0 0 0 0 0 0]
[ 39 50 61 0 0 0 0 0 0 0]
[ 44 55 0 0 0 0 0 0 0 0]
[ 50 0 0 0 0 0 0 0 0 0]]