MATLAB: Creating Diagonal Matrix from a Vector

matrixvector

I have a vector g = [g0 g1 g2 g3 … gx]
I want to create a matrix of the form:
Here x = (m-n)
Any thoughts on how I can do this?

Best Answer

The efficient MATLAB approach:
g = [1,2,3,4,5];
z = zeros(1,numel(g)-1);
m = toeplitz([g(1),z],[g,z])
m = 5×9
1 2 3 4 5 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0 1 2 3 4 5