Code
Code
UpperTrianglePart[A_?SquareMatrixQ,d_?IntegerQ]:=Block[{N_row},N_row=Length[A];Table[Join[Table[0,{j,1,Clip[i-1+d,{0,N_row}]}],A[[i,Clip[d+i,{1,N_row+1}];;]]],{i,1,N_row}]]
(Local) In[]:=
UpperTrianglePart[A_?SquareMatrixQ]:=UpperTrianglePart[A,0]
(Local) In[]:=
LowerTrianglePart[A_?SquareMatrixQ,d_?IntegerQ]:=Transpose[UpperTrianglePart[Transpose[A],d]]
(Local) In[]:=
LowerTrianglePart[A_?SquareMatrixQ]:=LowerTrianglePart[A,0]
Example
Example
(Local) In[]:=
A=Table[Symbol["a"<>ToString[i]<>ToString[j]],{i,1,3},{j,1,3}];
(Local) In[]:=
A//MatrixForm
(Local) Out[]//MatrixForm=
a11 | a12 | a13 |
a21 | a22 | a23 |
a31 | a32 | a33 |
(Local) In[]:=
UpperTrianglePart[A]//MatrixForm
(Local) Out[]//MatrixForm=
a11 | a12 | a13 |
0 | a22 | a23 |
0 | 0 | a33 |
(Local) In[]:=
UpperTrianglePart[A,-1]//MatrixForm
(Local) Out[]//MatrixForm=
a11 | a12 | a13 |
a21 | a22 | a23 |
0 | a32 | a33 |
(Local) In[]:=
UpperTrianglePart[A,-2]//MatrixForm
(Local) Out[]//MatrixForm=
a11 | a12 | a13 |
a21 | a22 | a23 |
a31 | a32 | a33 |
(Local) In[]:=
UpperTrianglePart[A,-3]//MatrixForm
(Local) Out[]//MatrixForm=
a11 | a12 | a13 |
a21 | a22 | a23 |
a31 | a32 | a33 |
(Local) In[]:=
UpperTrianglePart[A,1]//MatrixForm
(Local) Out[]//MatrixForm=
0 | a12 | a13 |
0 | 0 | a23 |
0 | 0 | 0 |
(Local) In[]:=
UpperTrianglePart[A,2]//MatrixForm
(Local) Out[]//MatrixForm=
0 | 0 | a13 |
0 | 0 | 0 |
0 | 0 | 0 |
(Local) In[]:=
UpperTrianglePart[A,3]//MatrixForm
(Local) Out[]//MatrixForm=
0 | 0 | 0 |
0 | 0 | 0 |
0 | 0 | 0 |
(Local) In[]:=
LowerTrianglePart[A]//MatrixForm
(Local) Out[]//MatrixForm=
a11 | 0 | 0 |
a21 | a22 | 0 |
a31 | a32 | a33 |
(Local) In[]:=
LowerTrianglePart[A,1]//MatrixForm
(Local) Out[]//MatrixForm=
0 | 0 | 0 |
a21 | 0 | 0 |
a31 | a32 | 0 |
(Local) In[]:=
LowerTrianglePart[A,-1]//MatrixForm
(Local) Out[]//MatrixForm=
a11 | a12 | 0 |
a21 | a22 | a23 |
a31 | a32 | a33 |