4 added 113 characters in body

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraAddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

# Compare with current acceptedciao's answerhere

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];


{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]


{1.41651, Null}

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraAddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

# Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];


{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]


{1.41651, Null}

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraAddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];


{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]


{1.41651, Null}

3 added 290 characters in body

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraAddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

# Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];


{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]


{1.41651, Null}

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraAddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

# Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

# Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[LinearAlgebraAddVectorToMatrixDiagonal[arg, -Diagonal[arg]];]


{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]


{1.41651, Null}

2 added 290 characters in body

# Method one:LinearAlgebraSetMatrixDiagonal

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

# Method two:LinearAlgebraAddVectorToMatrixDiagonal

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

# Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}

# Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

# Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraAddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

### Method one:LinearAlgebraSetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraSetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

### Method two:LinearAlgebraAddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebraAddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm


DeveloperPackedArrayQ[mat]


True

ps:Note this two method will change the original mat.

# Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebraSetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming


But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}`

1