Home > database >  How to make a lower triangle array of 10 but repeated across a diagonal n times?
How to make a lower triangle array of 10 but repeated across a diagonal n times?

Time:12-01

I am trying to create an array of 10 for each item I have, but then put those arrays of 10 into a larger array diagonally with zeros filling the missing spaces.

Here is an example of what I am looking for, but only with arrays of 3.

import numpy as np
arr = np.tri(3,3)
arr

This creates an array that looks like this:

[[1,0,0],
 [1,1,0],
 [1,1,1]]

But I need an array of 10 * n that looks like this: (using arrays a 3 for example here, with n=2)

{1,0,0,0,0,0, 1,1,0,0,0,0, 1,1,1,0,0,0, 0,0,0,1,0,0, 0,0,0,1,1,0, 0,0,0,1,1,1}

Any help would be appreciated, thanks!

I have also tried

df_arr2 = pd.concat([df_arr] * (n), ignore_index=True)
df_arr3 = pd.concat([df_arr2] *(n), axis=1, ignore_index=True)

But this repeats the matrix across all rows and columns, when I only want the diagnonal ones.

CodePudding user response:

Now I got it... AFAIU, the OP wants those np.tri triangles in the diagonal of a bigger, multiple of 3 square shaped array.

As per example, for n=2:

import numpy as np

n = 2

tri = np.tri(3)

arr = np.zeros((n*3, n*3))

for i in range(0, n*3, 3):
    arr[i:i 3,i:i 3] = tri

arr.astype(int)

# Out: 
# array([[1, 0, 0, 0, 0, 0],
#        [1, 1, 0, 0, 0, 0],
#        [1, 1, 1, 0, 0, 0],
#        [0, 0, 0, 1, 0, 0],
#        [0, 0, 0, 1, 1, 0],
#        [0, 0, 0, 1, 1, 1]])

CodePudding user response:

I saw @brandt's solution which is definitely the best. Incase you want to construct the them manually you can use this method:

def custom_triangle_matrix(rows, rowlen, tsize):
    cm = []
    for i in range(rows):
        row = []
        for j in range(min((i//tsize)*tsize, rowlen)):
            row.append(0)

        for j in range((i//tsize)*tsize, min(((i//tsize)*tsize)   i%tsize   1, rowlen)):
            row.append(1)

        for j in range(((i//tsize)*tsize)   i%tsize   1, rowlen):
            row.append(0)

        cm.append(row)

    return cm

Here are some example executions and what they look like using ppprint:

matrix = custom_triangle_matrix(6, 6, 3)
pprint.pprint(matrix)

[[1, 0, 0, 0, 0, 0],
 [1, 1, 0, 0, 0, 0],
 [1, 1, 1, 0, 0, 0],
 [0, 0, 0, 1, 0, 0],
 [0, 0, 0, 1, 1, 0],
 [0, 0, 0, 1, 1, 1]]

matrix = custom_triangle_matrix(6, 9, 3)
pprint.pprint(matrix)

[[1, 0, 0, 0, 0, 0, 0, 0, 0],
 [1, 1, 0, 0, 0, 0, 0, 0, 0],
 [1, 1, 1, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 1, 1, 0, 0, 0, 0],
 [0, 0, 0, 1, 1, 1, 0, 0, 0]]

matrix = custom_triangle_matrix(9, 6, 3)
pprint.pprint(matrix)

[[1, 0, 0, 0, 0, 0],
 [1, 1, 0, 0, 0, 0],
 [1, 1, 1, 0, 0, 0],
 [0, 0, 0, 1, 0, 0],
 [0, 0, 0, 1, 1, 0],
 [0, 0, 0, 1, 1, 1],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0]]

matrix = custom_triangle_matrix(10, 10, 5)
pprint.pprint(matrix)

[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
 [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
 [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
 [0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
 [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]]

Good Luck!

  • Related