[R] Help on comparing two matrices

Michael Kogan michael.kogan at gmx.net
Mon Aug 24 22:01:39 CEST 2009


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...

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.
>




More information about the R-help mailing list