[R] efficient matrix element comparison

2010-08-08 Thread david h shanabrook
It is a simple problem in that I simply want to convert the For loop to a more 
efficient method.  It simply loops through a large vector checking if the 
numeric element is not equal to the index of that element.  The following 
example demonstrates a simplified example:

 rows - 10
 collusionM - Matrix(0,10,10,sparse=TRUE)
 matchM - matrix(c(1,2,3,4,4,6,7,9,9,10))
 
 for (j in 1:rows) if (j != matchM[j]) collusionM[j,matchM[j]] - 
 collusionM[j,matchM[j]]+1
 collusionM
10 x 10 sparse Matrix of class dgCMatrix
 
 [1,] . . . . . . . . . .
 [2,] . . . . . . . . . .
 [3,] . . . . . . . . . .
 [4,] . . . . . . . . . .
 [5,] . . . 1 . . . . . .
 [6,] . . . . . . . . . .
 [7,] . . . . . . . . . .
 [8,] . . . . . . . . 1 .
 [9,] . . . . . . . . . .
[10,] . . . . . . . . . .

Again, this works, I just need the for loop to be more efficient as in my 
application rows=37000, and I need to do this 100 times.

Thanks.
[[alternative HTML version deleted]]

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


Re: [R] efficient matrix element comparison

2010-08-08 Thread Henrique Dallazuanna
Try this:

Matrix(diag(duplicated(matchM)), sparse = TRUE)

On Sun, Aug 8, 2010 at 9:43 PM, david h shanabrook
dhsha...@acad.umass.eduwrote:

 It is a simple problem in that I simply want to convert the For loop to a
 more efficient method.  It simply loops through a large vector checking if
 the numeric element is not equal to the index of that element.  The
 following example demonstrates a simplified example:

  rows - 10
  collusionM - Matrix(0,10,10,sparse=TRUE)
  matchM - matrix(c(1,2,3,4,4,6,7,9,9,10))
 
  for (j in 1:rows) if (j != matchM[j]) collusionM[j,matchM[j]] -
 collusionM[j,matchM[j]]+1
  collusionM
 10 x 10 sparse Matrix of class dgCMatrix

  [1,] . . . . . . . . . .
  [2,] . . . . . . . . . .
  [3,] . . . . . . . . . .
  [4,] . . . . . . . . . .
  [5,] . . . 1 . . . . . .
  [6,] . . . . . . . . . .
  [7,] . . . . . . . . . .
  [8,] . . . . . . . . 1 .
  [9,] . . . . . . . . . .
 [10,] . . . . . . . . . .

 Again, this works, I just need the for loop to be more efficient as in my
 application rows=37000, and I need to do this 100 times.

 Thanks.
[[alternative HTML version deleted]]

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

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


Re: [R] efficient matrix element comparison

2010-08-08 Thread Nikhil Kaza

How about

a - which(row(matchM)!=matchM)
b - matchM[a]
diag(collusionM[a,b]) -1


Nikhil Kaza
Asst. Professor,
City and Regional Planning
University of North Carolina

nikhil.l...@gmail.com

On Aug 8, 2010, at 8:43 PM, david h shanabrook wrote:

It is a simple problem in that I simply want to convert the For loop  
to a more efficient method.  It simply loops through a large vector  
checking if the numeric element is not equal to the index of that  
element.  The following example demonstrates a simplified example:



rows - 10
collusionM - Matrix(0,10,10,sparse=TRUE)
matchM - matrix(c(1,2,3,4,4,6,7,9,9,10))

for (j in 1:rows) if (j != matchM[j]) collusionM[j,matchM[j]] -  
collusionM[j,matchM[j]]+1

collusionM

10 x 10 sparse Matrix of class dgCMatrix

[1,] . . . . . . . . . .
[2,] . . . . . . . . . .
[3,] . . . . . . . . . .
[4,] . . . . . . . . . .
[5,] . . . 1 . . . . . .
[6,] . . . . . . . . . .
[7,] . . . . . . . . . .
[8,] . . . . . . . . 1 .
[9,] . . . . . . . . . .
[10,] . . . . . . . . . .

Again, this works, I just need the for loop to be more efficient as  
in my application rows=37000, and I need to do this 100 times.


Thanks.
[[alternative HTML version deleted]]

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


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