Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change 1d array to 2d array under certain conditions

Tags:

python

numpy

Is there a way through python numpy operations to produce the following result?

Input 1d array :

[3, 0, 0, 2, 2, 1]

Output 2d array :

3 0 0 2 2 1
0 0 0 2 2 1
0 0 0 2 2 1
2 2 2 2 2 1
2 2 2 2 2 1
1 1 1 1 1 1
like image 265
AIways Avatar asked Jun 01 '26 15:06

AIways


1 Answers

in addition to jeromie’s brilliant answer, here is a support for unordered array:

indexes = np.arange(len(arr))
idx = np.maximum(indexes[None,:], indexes[:, None])
arr[idx]
like image 153
adir abargil Avatar answered Jun 03 '26 04:06

adir abargil