NumPy triu keeps the upper triangle without sorting matrix values
A matrix has a shape, an arrangement of values and a set of positions. NumPy triu works with those positions. It returns a copy in which entries below a chosen diagonal become zero. The entries on or above that boundary keep their original values. The operation does not rank numbers, calculate a total or change the order of the rows.
This Academia Master Trader lesson is written for English-language study in Jamaica. The example uses a small invented matrix so that each position can be followed by hand. Its numbers are teaching values, not observations from a market or results from a trading method. The goal is to understand a clear array operation before using it in a larger calculation.
Start with a matrix you can read
Let a contain the rows [3,6,9], [12,15,18] and [21,24,27]. Its shape is (3,3). The first coordinate identifies a row and the second identifies a column. Both coordinates start at zero. The main diagonal therefore contains a[0,0], a[1,1] and a[2,2], whose values are 3,15 and 27.
The upper triangle includes that main diagonal and the positions above it. For this matrix, those positions contain 3,6,9,15,18 and 27. The values 12,21 and 24 sit below the main diagonal. Recognising the coordinates first makes it easier to explain exactly what triu will preserve.

Read the default result row by row
The expression np.triu(a) uses k=0. Its output has rows [3,6,9], [0,15,18] and [0,0,27]. The first row has no positions below the main diagonal, so all its values remain. In the second row, the first position is below the diagonal and becomes zero. In the third row, the first two positions become zero.
The shape stays (3,3). This is an important distinction from extracting a diagonal into a one-dimensional array. The result still has nine positions. Some of those positions now contain zeros, while the preserved entries occupy their original row and column coordinates. A triangular matrix is not a shorter list of the values that remain.
Use k to move the boundary
The parameter k selects the diagonal that defines the boundary. A positive value moves it above the main diagonal. With np.triu(a,k=1), the output rows are [0,6,9], [0,0,18] and [0,0,0]. The main diagonal is now below the selected boundary, so its entries also become zero.
A negative value moves the boundary below the main diagonal. With np.triu(a,k=-1), the rows are [3,6,9], [12,15,18] and [0,24,27]. Only the value 21 is below that boundary in this example. These three results differ because k changes the position rule, not because NumPy compares the sizes of the numbers.
Separate position from magnitude
The value 27 remains in the default result because it lies on the main diagonal. The value 21 becomes zero because it lies below that diagonal. Replacing 21 with a much smaller number would not change the positional rule. Replacing it with a much larger number would not change the rule either.
For k=0, an entry at row i and column j is preserved when j is greater than or equal to i. More generally, the selected upper region follows j minus i greater than or equal to k. This coordinate description is useful when checking a handwritten result because it explains each cell independently of the matrix values.
Keep the copy separate from the input
The NumPy documentation describes triu as returning a copy. Calling b=np.triu(a) therefore gives you a separate result to name and inspect. The operation does not mean that the input variable a has been rewritten. Keeping both names available makes the transformation easy to explain alongside the original matrix.
It also keeps the purpose of a zero clear. A zero already present in the input and a zero introduced below the boundary may look identical in the output. The result alone does not record which kind each zero was. If that distinction matters to your work, retain the input and describe the positional rule with the result.
Read rectangular and higher-dimensional inputs carefully
The operation does not require a square matrix. Rows and columns can have different lengths while the same diagonal boundary is applied to their positions. A drawing of a square matrix is a convenient introduction, but it should not be turned into a requirement that the function does not have.
For an array with more than two dimensions, the official documentation says triu applies to the final two axes. Identify those axes before describing the result. An earlier axis can distinguish separate matrix slices, while the last two axes define the row and column positions within each slice. This prevents a diagram of one matrix from being mistaken for the complete shape of a larger array.
Explain the transformation before continuing
A useful written explanation names the input shape, the value of k and the output rows. For this example, the default result preserves the main diagonal and entries above it, introduces zeros below it and keeps the same shape. That description is more informative than saying that the data has simply been reduced.
Continue your study with Academia Master Trader at academiamastertrader.com.br. Aprenda brings you to the educational material. Keep the original matrix beside the triangular copy and follow one coordinate at a time to see why each entry remains or becomes zero.