change diagonal elements of a matrix based on a formula in matlab code example Example: finding diagonal elements in matlab D = diag(v)