MATLAB: Is there a way to fill a matrix around the diagonals

diagonalefficientlinear algebraMATLABmatrix

I have a vector that I need to use to fill in the rows of a matrix. This vector will repeat over each row in the matrix except for the indices corresponding to the diagonals. Is there any way to do this without the need of a for loop?

Best Answer

What are the values for the diagonals? I assume you are talking about a square matrix. The below zeros out the diagonals.
x=repmat(v,length(v),1).*~eye(length(v)).*rot90(~eye(length(v)));
Related Question