[R] setting off-diagonals to zero

Daniel Nordlund djnordlund at frontier.com
Thu Jan 24 03:18:53 CET 2013


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of emorway
> Sent: Wednesday, January 23, 2013 4:59 PM
> To: r-help at r-project.org
> Subject: Re: [R] setting off-diagonals to zero
> 
> I'm not following.  Printing SEQ to the screen at the intermediate steps
> using the following modified R code suggests that 'i' is fine and is not
> getting reset to 1 as you suggest?  My understanding, or rather my desired
> output if someone else is able to weight-in, is that the values in the
> second line of output (731 732 733 etc.) should not be appearing in the
> 3rd
> line of output.  The third line of output should be missing 731 thru 736.
> Any suggestions on how to modify the R code are certainly welcome.
> Suggested revisions will be substituted back into the third FOR loop in my
> original post on this thread to prevent the main- and near-main-diagonal
> terms from being set equal to zero.
> 
> for (i in 731:732) {
>   SEQ <- (i - 5):(i + 5)
>   print(SEQ)
>   SEQ <- SEQ[SEQ > 730 & SEQ < 1096]
>   print(SEQ)
>   print((731:1095)[-SEQ])
> }
> 
> # [1] 726 727 728 729 730 731 732 733 734 735 736
> # [1] 731 732 733 734 735 736
> # [1]  731  732  733  734  735  736  737  738  739  740  741  742  743
> 744
> 745  746  747  748  749  750  751  752  753  754  755  756...
> 
> 
> 

The statement

print((731:1095)[-SEQ])

does not remove the values of SEQ (i.e. 731 732 733 734 735 736) from the printed sequence, but instead uses SEQ to index into the vector created by 731:1095.  But the vector 731:1095 has length 365, so no elements are removed because the smallest value in SEQ is 731 and there are not many elements in the vector.


Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA
 



More information about the R-help mailing list