Academia.eduAcademia.edu

vecTomat

Transforms a [n(n+1)/2] dimensional vector (containing the lower triangular part of a matrix) into a symmetric [nxn] matrix.

function [mat] = vecTomat(Q) % Transforms a vector into a square symmetric matrix % ---------------------------------------------------------------------- % INPUT % Q is a k(k+1)/2 vector containing the lower triangular part of a % symmetric matrix to be trasformed; % % OUTPUT % mat is a k x k symmetric matrix % ---------------------------------------------------------------------- % NB: % see also file ivechsymm.m written by Kevin Sheppard, but is slower. %----------------------------------------------------------------------- % Author: Manuela Braione, CORE 2014 K2=length(Q); K=(-1+sqrt(1+8*K2))/2; mat=zeros(K); pl=tril(true(K)); mat(pl)=Q; mat2prime=mat'; index_pr=find((triu(mat2prime, 1))); mat(index_pr)=mat2prime(index_pr);