Comparing and counting sign of elements in two matrics

sak

New Member
#1
I have two giant matrices (5000x6000) each. I want to compare the sign of elements of two matrices without doing a for loop kind of thing. And then I want to count how many had the matching sign.

I did find ways to compare whether two are identical but couldn't find anything which can do the above.

Thanks guys
 

Dason

Ambassador to the humans
#2
You definitely don't want to do this in a loop

Code:
# make fake matrices

nrow <- 4
matA <- matrix(rnorm(nrow^2), nrow, nrow)
matB <- matrix(rnorm(nrow^2), nrow, nrow)

# create logical matrix indicating if signs are equal
logmat <- sign(matA) == sign(matB)
# count how many are equal
sum(logmat)