Create a matrix, the lower triangle ( np.tril ) is filled with the values of your nums array and your upper triangle ( np.triu , with the second parameter 1, so the diagonal remains free) is filled with the maximum array. ( EDIT : instead of the maximum, the first element of the array is the best way. → comments)
nums = np.array([5.,3.,4.,2.,1.,1.,2.,0.]) oneSquare = np.ones((nums.size, nums.size)) A = nums * np.tril(oneSquare) B = np.triu(oneSquare, 1) * nums[0] A, B
Of:
(array([[ 5., 0., 0., 0., 0., 0., 0., 0.], [ 5., 3., 0., 0., 0., 0., 0., 0.], [ 5., 3., 4., 0., 0., 0., 0., 0.], [ 5., 3., 4., 2., 0., 0., 0., 0.], [ 5., 3., 4., 2., 1., 0., 0., 0.], [ 5., 3., 4., 2., 1., 1., 0., 0.], [ 5., 3., 4., 2., 1., 1., 2., 0.], [ 5., 3., 4., 2., 1., 1., 2., 0.]]), array([[ 0., 5., 5., 5., 5., 5., 5., 5.], [ 0., 0., 5., 5., 5., 5., 5., 5.], [ 0., 0., 0., 5., 5., 5., 5., 5.], [ 0., 0., 0., 0., 5., 5., 5., 5.], [ 0., 0., 0., 0., 0., 5., 5., 5.], [ 0., 0., 0., 0., 0., 0., 5., 5.], [ 0., 0., 0., 0., 0., 0., 0., 5.], [ 0., 0., 0., 0., 0., 0., 0., 0.]]))
Now take the minimum of each line:
(A+B).min(axis=1)
Of:
array([ 5., 3., 3., 2., 1., 1., 1., 0.])