vector to upper and lower triangular matrix
Show older comments
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;

Accepted Answer
More Answers (1)
David Hill
on 10 Nov 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 Comment
Edwin Chamba
on 10 Nov 2019
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!