MATLAB: How to superimpose a matrix

matrixmatrix manipulation

Two matrices (y and z, both are 2 x 2 matrix) are to be added to obtain third matrix (i.e. matrix 'x') which is 3×3.
However, x should be [ 1 -1 0; -1 2 -1; 0 -1 1]. Such that common index gets added and gives a final sum.
y = [1 -1; -1 1]
z = [1 -1; -1 1]

Best Answer

m = 5 ;
k0 = [1 2 2 2 1] ; % diagonal elements
k1 = -1*ones(1,m-1) ; % above and below main diagonal elements
iwant = diag(k1,-1)+diag(k0)+diag(k1,1) ;
Related Question