Search code examples
matlabmatrixvectorsymmetric

Create a Symmetric Matrix from a vector in MATLAB


How to covert vector A to symmetric matrix M in MATLAB

enter image description here

enter image description here

Such that M is a symmetric matrix (i.e. A21=A12) and all diagonal terms are equal (i.e. A11=A22=A33=A44).


Solution

  • Use hankel to help you create the symmetric matrix, then when you're finished, set the diagonal entries of this intermediate result to be the first element of the vector in A:

    M = hankel(A,A(end:-1:1));
    M(eye(numel(A))==1) = A(1);
    

    Example

    >> A = [1;2;3;4]
    
    A =
    
         1
         2
         3
         4
    
    >> M = hankel(A,A(end:-1:1));
    >> M(eye(numel(A))==1) = A(1)
    
    M =
    
         1     2     3     4
         2     1     4     3
         3     4     1     2
         4     3     2     1
    

    As you can see, M(i,j) = M(j,i) except for the diagonal, where each element is equal to A(1).