Try this:

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

On Sun, Aug 8, 2010 at 9:43 PM, david h shanabrook
<dhsha...@acad.umass.edu>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.
>



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

Reply via email to