0% found this document useful (0 votes)
139 views

MATLAB Central - Shortcut For Symmetric Matrices

Sasha asked for a way to easily create a symmetric matrix in MATLAB by inputting only the upper triangular elements as a vector. Several responses were provided: 1) Bruno recommended a MATLAB Central file exchange package containing a function to index the upper triangular elements of a matrix and suggested assigning the vector to this index and symmetrizing. 2) Jos provided an example using the TRIU function to create an upper triangular matrix, assign values from a vector, and add the transpose of the lower triangular part. 3) Paulo suggested a simple method of symmetrizing a triangular matrix by adding its transpose and subtracting the diagonal.

Uploaded by

Sepliong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

MATLAB Central - Shortcut For Symmetric Matrices

Sasha asked for a way to easily create a symmetric matrix in MATLAB by inputting only the upper triangular elements as a vector. Several responses were provided: 1) Bruno recommended a MATLAB Central file exchange package containing a function to index the upper triangular elements of a matrix and suggested assigning the vector to this index and symmetrizing. 2) Jos provided an example using the TRIU function to create an upper triangular matrix, assign values from a vector, and add the transpose of the lower triangular part. 3) Paulo suggested a simple method of symmetrizing a triangular matrix by adding its transpose and subtracting the diagonal.

Uploaded by

Sepliong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/17/2017 MATLABCentralShortcutforsymmetricmatrices

MATLABNewsgroup

Shortcutforsymmetricmatrices

Subject:Shortcutforsymmetricmatrices
From:Sasha
Date:14May,200910:37:37
Message:1of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
I'mtypingitinMatlabmanually.
Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
symmetricmatrixoutoftheuppertrianglememberssavedasavector?

IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
Thankyouallinadvance!

Subject:Shortcutforsymmetricmatrices
From:us
Date:14May,200910:46:01
Message:2of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

Sasha<[email protected]>wroteinmessage<[email protected]>...
>EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
>I'mtypingitinMatlabmanually.
>Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
>symmetricmatrixoutoftheuppertrianglememberssavedasavector?
>
>IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
>Thankyouallinadvance!

showCSSMasmall,typicalinputandtheresultyouexpect...

us

Subject:Shortcutforsymmetricmatrices
From:BrunoLuong
Date:14May,200910:50:04
Message:3of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

Sasha<[email protected]>wroteinmessage<[email protected]>...
>EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
>I'mtypingitinMatlabmanually.
>Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
>symmetricmatrixoutoftheuppertrianglememberssavedasavector?
>
>IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
>Thankyouallinadvance!

https://www.mathworks.com/matlabcentral/newsreader/view_thread/251343 1/5
1/17/2017 MATLABCentralShortcutforsymmetricmatrices

TakealookthispackageonFEX
http://www.mathworks.com/matlabcentral/fileexchange/23391

n=5
%Allocate
A=zeros(n)
i=itriu(size(A))%<thiscommandisfromthepackage

v=rand(size(i))%<inputsomewhere

%Assign
A(i)=v
%Symmetrize
Asym=A+triu(A,1).'

Bruno

Subject:Shortcutforsymmetricmatrices
From:Jos
Date:14May,200911:20:03
Message:4of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

Sasha<[email protected]>wroteinmessage<[email protected]>...
>EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
>I'mtypingitinMatlabmanually.
>Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
>symmetricmatrixoutoftheuppertrianglememberssavedasavector?
>
>IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
>Thankyouallinadvance!

Hereissomethingforyoutochewon:

V=[111222132333]

M=triu(ones(3))
M(M==1)=V
M=M+tril(M.',1)

hth
Jos

Subject:Shortcutforsymmetricmatrices
From:Sasha
Date:18May,200912:13:29
Message:5of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

Thankyouguysverymuch!It'snobigdifferencewhenthematrixis
small,butit'satimesaverifit'sabigone!
Ilikedbothideasandwillbeusingtheminfuture!

Iwishtoexpressmygratitudeoncemore!

https://www.mathworks.com/matlabcentral/newsreader/view_thread/251343 2/5
1/17/2017 MATLABCentralShortcutforsymmetricmatrices
OnMay14,1:20pm,"Jos"<#[email protected]>wrote:
>Sasha<[email protected]>wroteinmessage<d5d24296244a41d6986fac3=
[email protected]>...
>>EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
>>I'mtypingitinMatlabmanually.
>>Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
>>symmetricmatrixoutoftheuppertrianglememberssavedasavector?
>
>>IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
>>Thankyouallinadvance!
>
>Hereissomethingforyoutochewon:
>
>V=[111222132333]
>
>M=triu(ones(3))
>M(M==1)=V
>M=M+tril(M.',1)
>
>hth
>Jos

Subject:Shortcutforsymmetricmatrices
From:Raul
Date:12Nov,201016:47:05
Message:6of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

Sasha<[email protected]>wroteinmessage<[email protected]>...
>EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
>I'mtypingitinMatlabmanually.
>Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
>symmetricmatrixoutoftheuppertrianglememberssavedasavector?
>
>IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
>Thankyouallinadvance!

Hi!,

Haveyouthoughtinthe"toeplitz"command?
E.g.

x=

12345

>>toeplitz(x)

ans=

12345
21234
32123
43212
54321

Subject:Shortcutforsymmetricmatrices
From:Sean
Date:12Nov,201016:58:04
Message:7of9

https://www.mathworks.com/matlabcentral/newsreader/view_thread/251343 3/5
1/17/2017 MATLABCentralShortcutforsymmetricmatrices
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

>Haveyouthoughtinthe"toeplitz"command?
>E.g.
>
>x=
>
>12345
>
>>>toeplitz(x)
>
>ans=
>
>12345
>21234
>32123
>43212
>54321

Althoughtoeplitzdoescreateasymmetricmatrix,it'snotusablewhentheentireupperorlowerportionofthematrixisdifferent.

TheOPwantedtobeabletomanuallyenteronesideandhaveit"symmetricize"itautomatically.Likethis:

A=[123045002]%uppertriangular

'B=symmetricize(A)'
B=[123245352]

Subject:Shortcutforsymmetricmatrices
From:PauloOliveira
Date:24Apr,201417:10:09
Message:8of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

Sasha<[email protected]>wroteinmessage<[email protected]>...
>EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
>I'mtypingitinMatlabmanually.
>Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
>symmetricmatrixoutoftheuppertrianglememberssavedasavector?
>
>IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
>Thankyouallinadvance!

Iwouldsimplydothefollowing(assummingAisatriangularmatrix):

A=A+A'diag(diag(A))

Subject:Shortcutforsymmetricmatrices
From:StevenLord
Date:24Apr,201420:07:48
Message:9of9
Replytothismessage
AddauthortoMyWatchList
Vieworiginalformat
Flagasspam

https://www.mathworks.com/matlabcentral/newsreader/view_thread/251343 4/5
1/17/2017 MATLABCentralShortcutforsymmetricmatrices

"PauloOliveira"<[email protected]>wroteinmessage
news:[email protected]...
>Sasha<[email protected]>wroteinmessage
><[email protected]>...
>>EverytimeIhaveasymmetricmatrixtoinput,Ifeelridiculouswhile
>>I'mtypingitinMatlabmanually.
>>Couldsomebodygivemeanideaorapieceofcodethatwouldcreatea
>>symmetricmatrixoutoftheuppertrianglememberssavedasavector?
>>
>>IamabeginnerinMatlab,andanyadviceisgreatlyappreciated!
>>Thankyouallinadvance!
>
>Iwouldsimplydothefollowing(assummingAisatriangularmatrix):
>
>A=A+A'diag(diag(A))

Becareful.IfthediagonalofAcontainsnonfinitevalues,theywillbe
changedbythisoperation.CompareBandC:

A=magic(3)
A([1,5,9])=[Inf,Inf,NaN]
B=A+A'diag(diag(A))
C=triu(A,0)+triu(A,1).'

IfyouhaveStatisticsToolbox,theSQUAREFORMfunctionmayalsobeof
interest.


SteveLord
[email protected]
TocontactTechnicalSupportusetheContactUslinkon
http://www.mathworks.com

FeedforthisThread
AddtoMyWatchList

WhatisaWatchList?

TagsforthisThread
symmetry

Whataretags?
Atagislikeakeywordorcategorylabelassociatedwitheachthread.Tagsmakeiteasierforyoutofindthreadsofinterest.

Anyonecantagathread.Tagsarepublicandvisibletoeveryone.

19942017TheMathWorks,Inc.

https://www.mathworks.com/matlabcentral/newsreader/view_thread/251343 5/5

You might also like