MATLAB: I have a matrix which size is mxm (square) variable and I wanna assign a mx1 matrix into the first matrix’s diagonal. What kind of loop I have to write
matrix
for example;
%m=matrix's row which is variable depends on the input
a=zeros(m)
b=[mx1]
I wanna assign b matrix into the a matrix's diagonal.
Best Answer
This is very easy, if the off-diagonal element are always zeros:
m = 5;
b = rand(m,1);
a = diag(b);
But if you want a more general way to insertb along the diagonal of any arraya, then here's one way:
Best Answer