[R] Help on comparing two matrices

Gabor Grothendieck ggrothendieck at gmail.com
Tue Aug 25 15:35:27 CEST 2009


They can be regarded as incidence matrices rather than adjacency
matrices and in that case it follows:

library(igraph)

# incidence matrix to canonical edge list
inc2canel <- function(m) {
	g <- graph.incidence(m)
	cp <- canonical.permutation(g)
	can <- permute.vertices(g, cp$labeling)
	el <- get.edgelist(can)
	el[ order(el[,1], el[,2]), ]
}

# test it out

m1 <- matrix(c(0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1,
0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0,
0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1), 7)
m2 <- m1[7:1, 8:1]
m3 <- m2; m3[1, ] <- 1

identical(inc2canel(m1), inc2canel(m2)) # TRUE
identical(inc2canel(m1), inc2canel(m3)) # FALSE



On Sun, Aug 23, 2009 at 6:09 PM, Steve
Lianoglou<mailinglist.honeypot at gmail.com> wrote:
> Hi,
>
> On Sun, Aug 23, 2009 at 4:14 PM, Michael Kogan<michael.kogan at gmx.net> wrote:
>> Thanks for all the replies!
>>
>> Steve: I don't know whether my suggestion is a good one. I'm quite new to
>> programming, have absolutely no experience and this was the only one I could
>> think of. :-) I'm not sure whether I'm able to put your tips into practice,
>> unfortunately I had no time for much reading today but I'll dive into it
>> tomorrow.
>
> Ok, yeah. I'm not sure what the best way to do this myself, I would at
> first see if one could reduce these matrices by some principled manner
> and then do a comparison, which might jump to:
>
>> Ted: Wow, that's heavy reading. In fact the matrices that I need to compare
>> are incidence matrices so I suppose it's exactly the thing I need, but I
>> don't know if I have the basics knowledge to understand this paper within
>> the next months.
>
> Ted's sol'n. I haven't read the paper, but its title gives me an idea.
> Perhaps you can assume the two matrices you are comparing are
> adjacency matrices for a graph then use the igraph library to do a
> graph isomorphism test between the two graphs represented by your
> adjacency matrices and see if they are the same.
>
> This is probably not the most efficient (computationally) way to do
> it, but it might be the quickest way out coding-wise.
>
> I see your original example isn't using square matrices, and an
> adjacency matrix has to be square. Maybe you can pad your matrices
> with zero rows or columns (depending on what's deficient) as an easy
> way out.
>
> Just an idea.
>
> Of course, if David's solution is what you need, then no need to
> bother with any of this.
>
> -steve
>
> --
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
>  | Memorial Sloan-Kettering Cancer Center
>  | Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list