2.10 Generate a diagonal matrix with given values on the diagonal

Problem: generate diagonal matrix with \(2,4,6,8\) on the diagonal.

Mathematica

DiagonalMatrix[2 Range[1, 4]]
 

     {{2,0,0,0}, 
      {0,4,0,0}, 
      {0,0,6,0}, 
      {0,0,0,8}}
 

 

Matlab

diag(2*(1:4))
 

     2     0     0     0 
     0     4     0     0 
     0     0     6     0 
     0     0     0     8
 

 

Maple

A:=LinearAlgebra:-DiagonalMatrix([2,4,6,8]); 
#or 
A:=LinearAlgebra:-DiagonalMatrix( 
                       [seq(i,i=2..8,2)]);
 

[[2,0,0,0], 
 [0,4,0,0], 
 [0,0,6,0], 
 [0,0,0,8]]