vector to upper and lower triangular matrix
Mostrar comentarios más antiguos
Hey, I'm new to Matlab and wanted to know how to input a 1 by n matrix and output a n by n matrix such that the diagonals are the mean of the 1 by n matrix and the upper and lower trianglars are elements of that 1 by n matrix. I have it working for the case n = 3 and here is my code:
A = [ 5 4 3];
a = [0 A(1) A(2);
A(1) 0 A(3);
A(2) A(3) 0];
A_ij = mean(A)*eye(length(A),length(A)) + a;

Respuesta aceptada
Más respuestas (1)
David Hill
el 10 de Nov. de 2019
Not sure what elements you want above and below the diagonal.
a=repmat(A,[length(A),1]).*~diag(ones(1,length(A)))+mean(A)*eye(length(A));
1 comentario
Edwin Chamba
el 10 de Nov. de 2019
Categorías
Más información sobre Operating on Diagonal Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!