Thread Subject: symmetric matrix

Subject: symmetric matrix

From: Chris

Date: 2 Jun, 2009 16:19:02

Message: 1 of 25

Sorry for the elementary question, but is there a function I can call in MATLAB that would return 1 if a matrix is symmetric and zero if not?

Chris

Subject: symmetric matrix

From: us

Date: 2 Jun, 2009 16:37:02

Message: 2 of 25

"Chris " <solidsnake56982000@yahoo.com> wrote in message <h03jdm$aek$1@fred.mathworks.com>...
> Sorry for the elementary question, but is there a function I can call in MATLAB that would return 1 if a matrix is symmetric and zero if not?
>
> Chris

one of the many solutions

% the data
     m=[
          1 2 3
          2 9 4
          3 4 1
     ];
% a macro
     issym=@(x) all(all(tril(x)==triu(x).'));
     tf=issym(m)
% tf = 1
     m(1,3)=-10;
     tf=issym(m)
% tf = 0

us

Subject: symmetric matrix

From: Greg Heath

Date: 2 Jun, 2009 17:52:00

Message: 3 of 25

On Jun 2, 12:37 pm, "us " <u...@neurol.unizh.ch> wrote:
> "Chris " <solidsnake56982...@yahoo.com> wrote in message <h03jdm$ae...@fred.mathworks.com>...
> > Sorry for the elementary question, but is there a function I can call in MATLAB that would return 1 if a matrix is symmetric and zero if not?
>
> > Chris
>
> one of the many solutions
>
> % the data
>      m=[
>           1 2 3
>           2 9 4
>           3 4 1
>      ];
> % a macro
>      issym=@(x) all(all(tril(x)==triu(x).'));
>      tf=issym(m)
> %    tf = 1
>      m(1,3)=-10;
>      tf=issym(m)
> %    tf = 0
>
> us

issym=@(x) all(all(x==x.'));

is faster

Hope this helps.

Greg

Subject: symmetric matrix

From: us

Date: 2 Jun, 2009 19:17:01

Message: 4 of 25

Greg Heath
> issym=@(x) all(all(x==x.'));
> is faster

...and: SO MUCH smarter!

thanks, greg
us

Subject: symmetric matrix

From: Matt

Date: 2 Jun, 2009 19:32:01

Message: 5 of 25

"Chris " <solidsnake56982000@yahoo.com> wrote in message <h03jdm$aek$1@fred.mathworks.com>...
> Sorry for the elementary question, but is there a function I can call in MATLAB that would return 1 if a matrix is symmetric and zero if not?
>

The thing is, if a matrix is truly symmetric, it normally means it has been deliberately constructed that way by you, in a way that avoids floating point errors. So you would probably already know in advance if it were symmetric or not...

On the other hand, if you are testing the symmetry of a matrix that has been generated with floating point errors, you will have to deal with tolerance issues. Such a matrix won't be perfectly symmetryc and so the solutions proposed above won't be enough.

Subject: symmetric matrix

From: Jos

Date: 2 Jun, 2009 20:04:02

Message: 6 of 25

"us " <us@neurol.unizh.ch> wrote in message <h03trd$eun$1@fred.mathworks.com>...
> Greg Heath
> > issym=@(x) all(all(x==x.'));
> > is faster
>
> ...and: SO MUCH smarter!
>
> thanks, greg
> us

and may I suggest:

issym = @(x) isequal(x,x.')

Jos

Subject: symmetric matrix

From: Steven Lord

Date: 2 Jun, 2009 20:28:25

Message: 7 of 25


"Jos " <#10584@fileexchange.com> wrote in message
news:h040ji$h4e$1@fred.mathworks.com...
> "us " <us@neurol.unizh.ch> wrote in message
> <h03trd$eun$1@fred.mathworks.com>...
>> Greg Heath
>> > issym=@(x) all(all(x==x.'));
>> > is faster
>>
>> ...and: SO MUCH smarter!
>>
>> thanks, greg
>> us
>
> and may I suggest:
>
> issym = @(x) isequal(x,x.')

I like Jos's function better than Greg's, as it will return false instead of
erroring for nonsquare inputs. :)

There's lots more error checking you _could_ do in this function, but it's a
trade-off -- the more error checking, the more time it'll take when the
matrix is in fact symmetric.
    N-D arrays (check NDIMS)
    nonnumeric arrays (particularly structs, check ISNUMERIC or ISLOGICAL)
    NaNs (either ISNAN or ISEQUALWITHEQUALNANS -- and no I'm not making that
up.
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isequalwithequalnans.html)
    ...

--
Steve Lord
slord@mathworks.com

Subject: symmetric matrix

From: Kian

Date: 2 Jun, 2009 21:34:02

Message: 8 of 25

This may be a ridiculous method, but it works every time and it's not a macro.

isequal(size(x,1), size(x,2))

Subject: symmetric matrix

From: Matt

Date: 2 Jun, 2009 21:46:02

Message: 9 of 25

"Kian " <kian.torab@utah.edu> wrote in message <h045sa$7d6$1@fred.mathworks.com>...
> This may be a ridiculous method, but it works every time and it's not a macro.
>
> isequal(size(x,1), size(x,2))

This looks like a test of squareness rather than symmetry

Subject: symmetric matrix

From: Chris

Date: 4 Jun, 2009 13:00:18

Message: 10 of 25

Hey Guys,

Thanks for the feedback and responses, I chose to go with all(all(x == x')) because it seemed to be the fastest for my needs, but the discussion of different methods was very interesting and through this I have learned many new functions.

And the reason I need this is that I have a program that dumps out matrices and we were wanting to check and see if the matrices we have are what we expect to have, and one of the things we think the matrices should look like is symmetric.

Again thanks for the help.

Chris

Subject: symmetric matrix

From: Jos

Date: 4 Jun, 2009 13:19:01

Message: 11 of 25

"Chris " <solidsnake56982000@yahoo.com> wrote in message <h08gh2$3d8$1@fred.mathworks.com>...
> Hey Guys,
>
> Thanks for the feedback and responses, I chose to go with all(all(x == x')) because it seemed to be the fastest for my needs, but the discussion of different methods was very interesting and through this I have learned many new functions.

It seems that execution time depends on actual symmetry and size:

% try different m
m = eye(1000) ; m(1,end) = 1 ;
% m = rand(100)
% m = diag(1:100) ;

f1 = @() all(all(m==m.'))
f2 = @() isequal(m,m.')
t1 = timeit(f1)
t2 = timeit(f2)

Jos

Subject: symmetric matrix

From: D R

Date: 4 Jun, 2009 14:36:01

Message: 12 of 25

"Chris " <solidsnake56982000@yahoo.com> wrote in message <h03jdm$aek$1@fred.mathworks.com>...
> Sorry for the elementary question, but is there a function I can call in MATLAB that would return 1 if a matrix is symmetric and zero if not?
>
> Chris

If you want to check within a tolerance e (with e = 0 if you know you're dealing with exact cases), you could try
is_sym = max(max(A-A'))<=e

Subject: symmetric matrix

From: Bruno Luong

Date: 4 Jun, 2009 15:27:01

Message: 13 of 25

"D R" <d_a_n_r_o_p_1_removethisandunderscores@gmail.com> wrote in message <h08m4h$o0b$1@fred.mathworks.com>...

> is_sym = max(max(A-A'))<=e

It must have a missing ABS somewhere in the above. Otherwise I recommend

tol=1e-6; % for example
is_sum = norm(A-A.','fro') < tol*norm(A,'fro')

Bruno

Subject: symmetric matrix

From: D R

Date: 4 Jun, 2009 16:53:01

Message: 14 of 25

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h08p45$i1q$1@fred.mathworks.com>...
> "D R" <d_a_n_r_o_p_1_removethisandunderscores@gmail.com> wrote in message <h08m4h$o0b$1@fred.mathworks.com>...
>
> > is_sym = max(max(A-A'))<=e
>
> It must have a missing ABS somewhere in the above.

doh, how stupid of me... thanks for pointing it out.

is_sym = max(max(abs(A-A')))<=e

should work properly

Subject: symmetric matrix

From: D R

Date: 9 Jun, 2009 17:53:01

Message: 15 of 25

"D R" <d_a_n_r_o_p_1_removethisandunderscores@gmail.com> wrote in message <h08u5d$t8g$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h08p45$i1q$1@fred.mathworks.com>...
> > "D R" <d_a_n_r_o_p_1_removethisandunderscores@gmail.com> wrote in message <h08m4h$o0b$1@fred.mathworks.com>...
> >
> > > is_sym = max(max(A-A'))<=e
> >
> > It must have a missing ABS somewhere in the above.
>
> doh, how stupid of me... thanks for pointing it out.
>
> is_sym = max(max(abs(A-A')))<=e
>
> should work properly

Actually, I just realised
issym = max(max(A-A'))<=e
is actually sufficient (i.e. there is no need for abs()), because A-A' is antisymmetric.

Subject: symmetric matrix

From: D R

Date: 9 Jun, 2009 17:55:03

Message: 16 of 25

"D R" <d_a_n_r_o_p_1_removethisandunderscores@gmail.com> wrote in message <h08u5d$t8g$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h08p45$i1q$1@fred.mathworks.com>...
> > "D R" <d_a_n_r_o_p_1_removethisandunderscores@gmail.com> wrote in message <h08m4h$o0b$1@fred.mathworks.com>...
> >
> > > is_sym = max(max(A-A'))<=e
> >
> > It must have a missing ABS somewhere in the above.
>
> doh, how stupid of me... thanks for pointing it out.
>
> is_sym = max(max(abs(A-A')))<=e
>
> should work properly

Actually, I just realised
issym = max(max(A-A'))<=e
is actually sufficient (i.e. there is no need for abs()), because A-A' is antisymmetric.

Subject: symmetric matrix

From: Greg Heath

Date: 9 Jun, 2009 23:25:27

Message: 17 of 25

On Jun 9, 1:55 pm, "D R"
<d_a_n_r_o_p_1_removethisandundersco...@gmail.com> wrote:
> "D R" <d_a_n_r_o_p_1_removethisandundersco...@gmail.com> wrote in message <h08u5d$t8...@fred.mathworks.com>...
> > "Bruno Luong" <b.lu...@fogale.findmycountry> wrote in message <h08p45$i1...@fred.mathworks.com>...
> > > "D R" <d_a_n_r_o_p_1_removethisandundersco...@gmail.com> wrote in message <h08m4h$o0...@fred.mathworks.com>...
>
> > > > is_sym = max(max(A-A'))<=e
>
> > > It must have a missing ABS somewhere in the above.
>
> > doh, how stupid of me... thanks for pointing it out.
>
> > is_sym = max(max(abs(A-A')))<=e
>
> > should work properly
>
> Actually, I just realised
> issym = max(max(A-A'))<=e
> is actually sufficient (i.e. there is no need for abs()), because A-A' is antisymmetric.

But A-A' is antisymmetric only if A is symmetric.

Greg

Subject: symmetric matrix

From: Ken Campbell

Date: 10 Jun, 2009 00:25:04

Message: 18 of 25

"Chris " <solidsnake56982000@yahoo.com> wrote in message <h03jdm$aek$1@fred.mathworks.com>...
> Sorry for the elementary question, but is there a function I can call in MATLAB that would return 1 if a matrix is symmetric and zero if not?
>
> Chris

I'm a little surprised that there isn't a MATLAB built-in function "issymmetric". There seem to be at least 50 other is-somethings included in my installation.

Is issymmetric something that would be worth requesting as an "enhancement" ?

Ken

Subject: symmetric matrix

From: Ken Campbell

Date: 10 Jun, 2009 00:40:17

Message: 19 of 25

"Chris " <solidsnake56982000@yahoo.com> wrote in message <h03jdm$aek$1@fred.mathworks.com>...
> Sorry for the elementary question, but is there a function I can call in MATLAB that would return 1 if a matrix is symmetric and zero if not?
>
> Chris

I'm a little surprised that there isn't a MATLAB built-in function "issymmetric". There seem to be at least 50 other is-somethings included in my installation.

Is issymmetric something that would be worth requesting as an "enhancement" ?

Ken

Subject: symmetric matrix

From: Bruno Luong

Date: 10 Jun, 2009 05:56:58

Message: 20 of 25

Greg Heath <heath@alumni.brown.edu> wrote in message <2d22fe76-6be5-4938-ae58-ecd520d5a34e@s16g2000vbp.googlegroups.com>...

>
> But A-A' is antisymmetric only if A is symmetric.
>

A-A' is *conjugate* antisymmetric in general, antisymetric when A is real, zero when A is symmetric.

Bruno

Subject: symmetric matrix

From: Bobby Cheng

Date: 10 Jun, 2009 13:32:32

Message: 21 of 25

http://en.wikipedia.org/wiki/Skew-Hermitian_matrix

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:h0nhv9$dq9$1@fred.mathworks.com...
> Greg Heath <heath@alumni.brown.edu> wrote in message
> <2d22fe76-6be5-4938-ae58-ecd520d5a34e@s16g2000vbp.googlegroups.com>...
>
>>
>> But A-A' is antisymmetric only if A is symmetric.
>>
>
> A-A' is *conjugate* antisymmetric in general, antisymetric when A is real,
> zero when A is symmetric.
>
> Bruno

Subject: symmetric matrix

From: D R

Date: 10 Jun, 2009 14:49:02

Message: 22 of 25

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h0nhv9$dq9$1@fred.mathworks.com>...
> Greg Heath <heath@alumni.brown.edu> wrote in message <2d22fe76-6be5-4938-ae58-ecd520d5a34e@s16g2000vbp.googlegroups.com>...
>
> >
> > But A-A' is antisymmetric only if A is symmetric.
> >
>
> A-A' is *conjugate* antisymmetric in general, antisymetric when A is real, zero when A is symmetric.
>
> Bruno

Thanks for pointing that out; although, if the matrix is complex (I'd only been thinking about real matrices) I imagine one sometimes need to be careful as to how to characterise closeness of two complex terms: for eg, two small vectors of equal magnitude pointing in opposite directions? I surmise that abs() might still be sufficient for most algorithms.

Subject: symmetric matrix

From: D R

Date: 10 Jun, 2009 14:56:03

Message: 23 of 25


> Thanks for pointing that out; although, if the matrix is complex (I'd only been thinking about real matrices) I imagine one sometimes need to be careful as to how to characterise closeness of two complex terms: for eg, two small vectors of equal magnitude pointing in opposite directions? I surmise that abs() might still be sufficient for most algorithms.

Assuming by symmetric the OP means conjugate symmetric for a complex matrix.

Subject: symmetric matrix

From: Ondrej

Date: 23 Mar, 2011 10:24:04

Message: 24 of 25

according to my simulation the fastest way is to do this:

issym = nnz(A-A')>0;

Subject: symmetric matrix

From: Matt J

Date: 23 Mar, 2011 10:45:21

Message: 25 of 25

"Ondrej" wrote in message <imcho4$b6d$1@fred.mathworks.com>...
> according to my simulation the fastest way is to do this:
>
> issym = nnz(A-A')>0;
===============

That might be true for sparse A, but not for full (see below). Also, your method doesn't include floating point error tolerances, the need for which was discussed in the previous posts...


A=rand(4000);

tic
issym = nnz(A-A')>0;
toc;
%Elapsed time is 0.229682 seconds.
 
 tic;
 issym=isequal(A,A.');
 toc;
%Elapsed time is 0.144698 seconds.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
macro us 2 Jun, 2009 13:36:13
tril us 2 Jun, 2009 12:39:06
all us 2 Jun, 2009 12:39:06
triu us 2 Jun, 2009 12:39:06
code us 2 Jun, 2009 12:39:06
rssFeed for this Thread

Contact us at files@mathworks.com