Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the purpose of setting the "diagonal" elements to zero you have already been given an answer but I wonder if you were hoping for something more general. The reasons for lack of success with that code were two-fold: the construction of your indices were flawed and the indexing was wrong. This would have succeeded:</p> <pre><code>for(i in 1:(rowCount - 1)){ # need an expression that retruns a sequence for (j in 1:rowCount) # ditto if (i == j){ similMatrix[i,j] &lt;- 0; # need to index the matrix with two element if using i,j } } #---------- &gt; show(similMatrix) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 0 NA NA NA NA NA NA NA NA [2,] NA 0 NA NA NA NA NA NA NA [3,] NA NA 0 NA NA NA NA NA NA [4,] NA NA NA 0 NA NA NA NA NA [5,] NA NA NA NA 0 NA NA NA NA [6,] NA NA NA NA NA 0 NA NA NA [7,] NA NA NA NA NA NA 0 NA NA [8,] NA NA NA NA NA NA NA 0 NA </code></pre> <p>But resorting to loops in R is generally considered a last resort (sometimes for the wrong reasons.) There is a much more compact way of doing the same "loop" operation and it generalizes more widely than just setting the diagonal.</p> <pre><code>similMatrix[ row(similMatrix) == col(similMatrix) ] &lt;- 0 &gt; similMatrix [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 0 NA NA NA NA NA NA NA NA [2,] NA 0 NA NA NA NA NA NA NA [3,] NA NA 0 NA NA NA NA NA NA [4,] NA NA NA 0 NA NA NA NA NA [5,] NA NA NA NA 0 NA NA NA NA [6,] NA NA NA NA NA 0 NA NA NA [7,] NA NA NA NA NA NA 0 NA NA [8,] NA NA NA NA NA NA NA 0 NA </code></pre> <p>If you wanted to set the subdiagonal to zero you could just use:</p> <pre><code>similMatrix[ row(similMatrix)-1 == col(similMatrix) ] &lt;- 0 </code></pre> <p>You can avoid generating the extra row and col matrices using this:</p> <pre><code> mind &lt;- min( dim(similMatrix) ) # avoid going outside dimensions if not symmetric similMatrix[ cbind( seq(maxd),seq(maxd) ) &lt;- 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload