On Aug 24, 2009, at 4:01 PM, Michael Kogan wrote:
David: Well, e.g. the first row has 2 ones in your output while
there were no rows with 2 ones in the original matrix. Since the row
and column sums can't be changed by sorting them, the output matrix
can't be equivalent to the original one. But that means nothing,
maybe it's intended and just for comparison reasons? :) But I don't
get how the ones can get lost by making a string out of the row
values...
OK, so shoot me. I screwed up and forgot to use byrow=TRUE in my scan
operation. So I ended up with a different starting matrix than you.
This is what it should have looked like:
> sm <- matrix(scan(textConnection("
+ 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, 8, byrow=TRUE)
Read 56 items
> sm
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0 1 1 1 0 1 1 0
[2,] 1 1 0 0 0 1 0 1
[3,] 1 0 1 0 0 0 1 1
[4,] 1 1 0 0 1 0 0 0
[5,] 1 0 1 1 1 0 0 0
[6,] 0 1 0 1 1 0 0 0
[7,] 0 0 0 0 0 1 1 1
> order(sapply(1:7, function(x) Reduce(paste, sm[x,])) )
[1] 7 6 1 3 5 2 4
> sm[order(sapply(1:7, function(x) Reduce(paste, sm[x,])) ), ]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0 0 0 0 0 1 1 1
[2,] 0 1 0 1 1 0 0 0
[3,] 0 1 1 1 0 1 1 0
[4,] 1 0 1 0 0 0 1 1
[5,] 1 0 1 1 1 0 0 0
[6,] 1 1 0 0 0 1 0 1
[7,] 1 1 0 0 1 0 0 0
The process creates a sorted index and then just outputs rows from the
original matrix, so there cannot be any row that was not there at the
start. Gabor's solution will do the same operation and certainly looks
more elegant than mine. (His input operation did the same mutilation
on your input string as did mine.)
--
David
Steve: The two matrices I want to compare really are graph matrices,
just not adjacency but incidence matrices. There should be a way to
get an adjacency matrix of a graph out of its incidence matrix but I
don't know it...
David Winsemius schrieb:
On Aug 23, 2009, at 4:14 PM, Michael Kogan 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.
David: To be honest I don't understand your code yet, but the
result is not equivalent to the original matrix since the row sums
don't match, isn't it? Or is it intended like this? I'll try to
ask Google (or rather RSeek) tomorrow to understand your code. :-)
Not sure what you mean by the "row sums don't match.
All I did (working from the inside of that function outward) was:
a) concatenate the row values into a string:
> Reduce(paste, sm[1,])
[1] "0 0 0 0 1 1 0 0" # the innermost function applied to the
first row.
b) do it for every row
> sapply(1:7, function(x) Reduce(paste, sm[x,]))
[1] "0 0 0 0 1 1 0 0" "1 1 1 1 0 1 1 0" "1 1 1 1 0 0 1 0" "1 0 0 1
0 0 0 0"
"0 0 1 1 1 0 0 1"
[6] "1 0 0 0 0 0 0 1" "1 1 0 0 1 1 0 1"
c) create a sorting vector from that vector (of characters):
> order(sapply(1:7, function(x) Reduce(paste, sm[x,])) )
[1] 1 5 6 4 7 3 2
d) use that sort vector to order the rows:
> sm[order(sapply(1:7, function(x) Reduce(paste, sm[x,])) ), ]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0 0 0 0 1 1 0 0
[2,] 0 0 1 1 1 0 0 1
[3,] 1 0 0 0 0 0 0 1
[4,] 1 0 0 1 0 0 0 0
[5,] 1 1 0 0 1 1 0 1
[6,] 1 1 1 1 0 0 1 0
[7,] 1 1 1 1 0 1 1 0
All of the original vectors are output, just in a different order,
so I am therefore confused..... why you think the "rowSums don't
match" .... don't match what?
I assumed you would take this process and apply it to _each_ of the
two matrices in question and then see if you got a TRUE result with
the identical function or perhaps the "==" function.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
R-help@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.