[R] Help on comparing two matrices

Michael Kogan michael.kogan at gmx.net
Tue Aug 25 11:44:46 CEST 2009


David: Ah, so that was the reason! I didn't realize that. :) Ok, so I 
try to go through the code and understand it:

The last line seems to bring the rows into the order given by the 
"order" command. But how does the order command get the order? Lets look 
into the order function: the Reduce function is applied to all seven 
rows by the sapply command. Which itself  does what?  Reduce(paste, 
sm[x,]) seems to "paste" all rows while paste means just "write next to 
each other as string". So we get one big string consisting of all the 7 
rows inside the order function. But how does the order function get this 
order [1] 7 6 1 3 5 2 4 out of this big string? Sorry, I'm doing my best 
to understand it... :(

Daniel: I think I understand your idea (though not the code yet). So if 
the matrix dimensions are x=x3+x4+x5... and y=y3+y4+y5... (where x_i is 
the number of rows with the sum i) then x3!x4!x5!...y3!y4!y5!... 
combinations have to be checked in the worst case if the matrices are 
equivalent (or in each case if they're not). I think it's a bit 
computationally intensive (since the comparison algorithm has to be used 
many times on many matrices) but if everything else fails, it will do. :)

Thanks to all for your help!
Michael

David Winsemius schrieb:
>
> 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.)
>




More information about the R-help mailing list