

The following 1460 x 1460 matrix can be throught of as 16 distinct 365 x 365 matrices. I'm trying to set offdiaganol terms in the 16 submatrices with indices more than +/ 5 (days) from each other to zero using some for loops. This works well for some, but not all, of the for loops. The R code I"m using follows. For some reason the third loop below zero'sout everything in the subquadrant it is targeting, which is readily observable when viewing the matrix ("View(MAT)").
library(Matrix)
MAT<matrix(rnorm(1460*1460,mean=0,sd=1),nrow = 1460, ncol = 1460)
#works great
for (i in 1:365) {
SEQ < (i  5):(i + 5)
SEQ < SEQ[SEQ > 0 & SEQ < 366]
MAT[(1:365)[SEQ], i] < 0
}
#works great
for (i in 1:365) {
SEQ < (i  5):(i + 5)
SEQ < SEQ[SEQ > 0 & SEQ < 366]
MAT[(1:365)[SEQ], i + 365] < 0
}
#zero's out everything, including maindiagonal and nearmaindiagonal terms???
for (i in 731:1095) {
SEQ < (i  5):(i + 5)
SEQ < SEQ[SEQ > 730 & SEQ < 1096]
MAT[(731:1095)[SEQ], i + 365] < 0
}
View(MAT)
I'm not sure why the third FOR loop above is not leaving the maindiagonal and nearmaindiagonal terms alone?


On 23012013, at 16:13, emorway < [hidden email]> wrote:
> The following 1460 x 1460 matrix can be throught of as 16 distinct 365 x 365
> matrices. I'm trying to set offdiaganol terms in the 16 submatrices with
> indices more than +/ 5 (days) from each other to zero using some for loops.
> This works well for some, but not all, of the for loops. The R code I"m
> using follows. For some reason the third loop below zero'sout everything
> in the subquadrant it is targeting, which is readily observable when
> viewing the matrix ("View(MAT)").
>
> library(Matrix)
> MAT<matrix(rnorm(1460*1460,mean=0,sd=1),nrow = 1460, ncol = 1460)
>
> #works great
> for (i in 1:365) {
> SEQ < (i  5):(i + 5)
> SEQ < SEQ[SEQ > 0 & SEQ < 366]
> MAT[(1:365)[SEQ], i] < 0
> }
>
> #works great
> for (i in 1:365) {
> SEQ < (i  5):(i + 5)
> SEQ < SEQ[SEQ > 0 & SEQ < 366]
> MAT[(1:365)[SEQ], i + 365] < 0
> }
>
> #zero's out everything, including maindiagonal and nearmaindiagonal
> terms???
> for (i in 731:1095) {
> SEQ < (i  5):(i + 5)
> SEQ < SEQ[SEQ > 730 & SEQ < 1096]
> MAT[(731:1095)[SEQ], i + 365] < 0
> }
>
> View(MAT)
>
> I'm not sure why the third FOR loop above is not leaving the maindiagonal
> and nearmaindiagonal terms alone?
Because SEQ in the last expression is not referencing the correct elements.
The first element of (731:1009) has index 1 and not 731.
So as far as I can see the last expression should be something like
MAT[(731:1095)[(730+SEQ)], i + 365] < 0
Berend
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 weightin, 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 nearmaindiagonal 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...


On Jan 23, 2013, at 7:13 AM, emorway wrote:
> The following 1460 x 1460 matrix can be throught of as 16 distinct 365 x 365
> matrices. I'm trying to set offdiaganol terms in the 16 submatrices with
> indices more than +/ 5 (days) from each other to zero using some for loops.
> This works well for some, but not all, of the for loops. The R code I"m
> using follows. For some reason the third loop below zero'sout everything
> in the subquadrant it is targeting, which is readily observable when
> viewing the matrix ("View(MAT)").
>
> library(Matrix)
> MAT<matrix(rnorm(1460*1460,mean=0,sd=1),nrow = 1460, ncol = 1460)
>
The way to do that in a single 365 x 365 matrix is:
Mat < matrix( 1:(365*365), 365, 365)
Mat[ abs( col(Mat)row(Mat) ) > 5 ] < 0
Mat
The way to propagate that pattern is to use rep(), so here is a oneliner for the task:
MAT[ rep( abs( col(Mat)row(Mat) ) > 5, 16) ] < 0
Didn't test on you gigantuan matrix; used smaller example:
Mat < matrix( 1:(16*16), 16, 16)
test < rbind(Mat, Mat)
test[rep( abs( col(Mat)row(Mat) ) > 2 , 2)] < 0
test

David.
> #works great
> for (i in 1:365) {
> SEQ < (i  5):(i + 5)
> SEQ < SEQ[SEQ > 0 & SEQ < 366]
> MAT[(1:365)[SEQ], i] < 0
> }
>
> #works great
> for (i in 1:365) {
> SEQ < (i  5):(i + 5)
> SEQ < SEQ[SEQ > 0 & SEQ < 366]
> MAT[(1:365)[SEQ], i + 365] < 0
> }
>
> #zero's out everything, including maindiagonal and nearmaindiagonal
> terms???
> for (i in 731:1095) {
> SEQ < (i  5):(i + 5)
> SEQ < SEQ[SEQ > 730 & SEQ < 1096]
> MAT[(731:1095)[SEQ], i + 365] < 0
> }
>
> View(MAT)
>
> I'm not sure why the third FOR loop above is not leaving the maindiagonal
> and nearmaindiagonal terms alone?
>
>
>
> 
> View this message in context: http://r.789695.n4.nabble.com/settingoffdiagonalstozerotp4656407.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
David Winsemius
Alameda, CA, USA
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


> Original Message
> From: [hidden email] [mailto: [hidden email]]
> On Behalf Of emorway
> Sent: Wednesday, January 23, 2013 4:59 PM
> To: [hidden email]
> Subject: Re: [R] setting offdiagonals 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 weightin, 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 nearmaindiagonal
> 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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


HI,
Not sure this is what you wanted.
for (i in 731:732) {
SEQ < (i  5):(i + 5)
print(SEQ)
SEQ < SEQ[SEQ > 730 & SEQ < 1096]
print(SEQ)
vec1<731:741
print(vec1[!vec1%in%SEQ])
}
#[1] 726 727 728 729 730 731 732 733 734 735 736
#[1] 731 732 733 734 735 736
#[1] 737 738 739 740 741
# [1] 727 728 729 730 731 732 733 734 735 736 737
#[1] 731 732 733 734 735 736 737
#[1] 738 739 740 741
A.K.
 Original Message 
From: emorway < [hidden email]>
To: [hidden email]
Cc:
Sent: Wednesday, January 23, 2013 7:58 PM
Subject: Re: [R] setting offdiagonals 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 weightin, 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 nearmaindiagonal
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...

View this message in context: http://r.789695.n4.nabble.com/settingoffdiagonalstozerotp4656407p4656461.htmlSent from the R help mailing list archive at Nabble.com.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On 24012013, at 01:58, emorway < [hidden email]> wrote:
> 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?
You misread. I did not say anything about 'i'.
> My understanding, or rather my desired
> output if someone else is able to weightin, 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.
Well then look at this
for (i in 731:732) {
SEQ < (i  5):(i + 5)
print(SEQ)
SEQ < SEQ[SEQ > 730 & SEQ < 1096]
print(SEQ)
print((731:1095)[(730+SEQ)]) # my modification
}
# [1] 726 727 728 729 730 731 732 733 734 735 736
# [1] 731 732 733 734 735 736
# [1] 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
which seems to give the desired output
> 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 nearmaindiagonal
> 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...
>
>
>
>
> 
> View this message in context: http://r.789695.n4.nabble.com/settingoffdiagonalstozerotp4656407p4656461.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On Jan 23, 2013, at 5:31 PM, David Winsemius wrote:
>
> On Jan 23, 2013, at 7:13 AM, emorway wrote:
>
>> The following 1460 x 1460 matrix can be throught of as 16 distinct
>> 365 x 365
>> matrices. I'm trying to set offdiaganol terms in the 16 sub
>> matrices with
>> indices more than +/ 5 (days) from each other to zero using some
>> for loops.
>> This works well for some, but not all, of the for loops. The R
>> code I"m
>> using follows. For some reason the third loop below zero'sout
>> everything
>> in the subquadrant it is targeting, which is readily observable when
>> viewing the matrix ("View(MAT)").
>>
>> library(Matrix)
>> MAT<matrix(rnorm(1460*1460,mean=0,sd=1),nrow = 1460, ncol = 1460)
>>
>
> The way to do that in a single 365 x 365 matrix is:
>
> Mat < matrix( 1:(365*365), 365, 365)
> Mat[ abs( col(Mat)row(Mat) ) > 5 ] < 0
> Mat
>
> The way to propagate that pattern is to use rep(), so here is a one
> liner for the task:
>
> MAT[ rep( abs( col(Mat)row(Mat) ) > 5, 16) ] < 0
That might not construct the bands in the blocks correctly: This might
be better:
MAT [ kronecker( matrix(1, 4,4), abs( col(Mat)row(Mat) ) > 5,
FUN="&" ) ] < 0

David.
>
>
> Didn't test on you gigantuan matrix; used smaller example:
>
> Mat < matrix( 1:(16*16), 16, 16)
> test < rbind(Mat, Mat)
> test[rep( abs( col(Mat)row(Mat) ) > 2 , 2)] < 0
> test
>
> 
> David.
>
>
>> #works great
>> for (i in 1:365) {
>> SEQ < (i  5):(i + 5)
>> SEQ < SEQ[SEQ > 0 & SEQ < 366]
>> MAT[(1:365)[SEQ], i] < 0
>> }
>>
>> #works great
>> for (i in 1:365) {
>> SEQ < (i  5):(i + 5)
>> SEQ < SEQ[SEQ > 0 & SEQ < 366]
>> MAT[(1:365)[SEQ], i + 365] < 0
>> }
>>
>> #zero's out everything, including maindiagonal and nearmain
>> diagonal
>> terms???
>> for (i in 731:1095) {
>> SEQ < (i  5):(i + 5)
>> SEQ < SEQ[SEQ > 730 & SEQ < 1096]
>> MAT[(731:1095)[SEQ], i + 365] < 0
>> }
>>
>> View(MAT)
>>
>> I'm not sure why the third FOR loop above is not leaving the main
>> diagonal
>> and nearmaindiagonal terms alone?
>>
>>
>>
>> 
>> View this message in context: http://r.789695.n4.nabble.com/settingoffdiagonalstozerotp4656407.html>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
David Winsemius, MD
Alameda, CA, USA
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


useRs,
Using a code suggestion from the previous post in this thread (quoted
next), I was able to work through a smaller example problem and get some
results I was aiming for.
>That might not construct the bands in the blocks correctly: This might
>be better:
>
>MAT [ kronecker( matrix(1, 4,4), abs( col(Mat)row(Mat) ) > 5,
>FUN="&" ) ] < 0
With this working example in hand, I tried applying the R code to the larger
problem but ran into 'allocate' error. The small working example (next) is
followed by the larger problem that kicks back an error message. I'm
hopeful someone may know of a workaround for avoiding the allocate error?
##Small example
Mat < matrix( 1:(4*4), 4, 4)
test < rbind(Mat, Mat)
test2< cbind(test,test)
#test2 is a 8x8 matrix with 4 4x4 submatrices
#The following preserves the diagonal bands I want to keep (has TRUE where
desired)
edm<!kronecker( matrix(1, 4,4), abs( col(Mat)row(Mat) ) > 1, FUN="&" )
#However, the size was doubled in the above, so here I keep only that portion
that is the same size as test2
edm<edm[1:8,1:8]
#The following retains the values I'm interested in.
test3<ifelse(edm,test2,0)
##Repeat the process on the larger problem which contains 16 365 x 365
submatrices
Mat < matrix( 1:(365*365), 365, 365)
test < rbind(Mat, Mat, Mat, Mat)
test2< cbind(test,test,test,test)
edm<!kronecker( matrix(1, 365,365), abs( col(Mat)row(Mat) ) > 1, FUN="&" )
#Error: cannot allocate vector of size 4.2 Gb
edm<edm[1:1460,1:1460]
Is there a way around this?
Thanks, Eric
On Wed, Jan 23, 2013 at 11:32 PM, David Winsemius < [hidden email]>wrote:
>
> On Jan 23, 2013, at 5:31 PM, David Winsemius wrote:
>
>
>> On Jan 23, 2013, at 7:13 AM, emorway wrote:
>>
>> The following 1460 x 1460 matrix can be throught of as 16 distinct 365 x
>>> 365
>>> matrices. I'm trying to set offdiaganol terms in the 16 submatrices
>>> with
>>> indices more than +/ 5 (days) from each other to zero using some for
>>> loops.
>>> This works well for some, but not all, of the for loops. The R code I"m
>>> using follows. For some reason the third loop below zero'sout
>>> everything
>>> in the subquadrant it is targeting, which is readily observable when
>>> viewing the matrix ("View(MAT)").
>>>
>>> library(Matrix)
>>> MAT<matrix(rnorm(1460*1460,**mean=0,sd=1),nrow = 1460, ncol = 1460)
>>>
>>>
>> The way to do that in a single 365 x 365 matrix is:
>>
>> Mat < matrix( 1:(365*365), 365, 365)
>> Mat[ abs( col(Mat)row(Mat) ) > 5 ] < 0
>> Mat
>>
>> The way to propagate that pattern is to use rep(), so here is a oneliner
>> for the task:
>>
>> MAT[ rep( abs( col(Mat)row(Mat) ) > 5, 16) ] < 0
>>
>
> That might not construct the bands in the blocks correctly: This might be
> better:
>
> MAT [ kronecker( matrix(1, 4,4), abs( col(Mat)row(Mat) ) > 5, FUN="&" ) ]
> < 0
>
> 
> David.
>
>
>>
>> Didn't test on you gigantuan matrix; used smaller example:
>>
>> Mat < matrix( 1:(16*16), 16, 16)
>> test < rbind(Mat, Mat)
>> test[rep( abs( col(Mat)row(Mat) ) > 2 , 2)] < 0
>> test
>>
>> 
>> David.
>>
>>
>> #works great
>>> for (i in 1:365) {
>>> SEQ < (i  5):(i + 5)
>>> SEQ < SEQ[SEQ > 0 & SEQ < 366]
>>> MAT[(1:365)[SEQ], i] < 0
>>> }
>>>
>>> #works great
>>> for (i in 1:365) {
>>> SEQ < (i  5):(i + 5)
>>> SEQ < SEQ[SEQ > 0 & SEQ < 366]
>>> MAT[(1:365)[SEQ], i + 365] < 0
>>> }
>>>
>>> #zero's out everything, including maindiagonal and nearmaindiagonal
>>> terms???
>>> for (i in 731:1095) {
>>> SEQ < (i  5):(i + 5)
>>> SEQ < SEQ[SEQ > 730 & SEQ < 1096]
>>> MAT[(731:1095)[SEQ], i + 365] < 0
>>> }
>>>
>>> View(MAT)
>>>
>>> I'm not sure why the third FOR loop above is not leaving the
>>> maindiagonal
>>> and nearmaindiagonal terms alone?
>>>
>>>
>>>
>>> 
>>> View this message in context: http://r.789695.n4.nabble.com/**>>> settingoffdiagonalstozero**tp4656407.html< http://r.789695.n4.nabble.com/settingoffdiagonalstozerotp4656407.html>
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________**________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/rhelp< https://stat.ethz.ch/mailman/listinfo/rhelp>
>>> PLEASE do read the posting guide http://www.Rproject.org/**>>> postingguide.html < http://www.Rproject.org/postingguide.html>
>>> and provide commented, minimal, selfcontained, reproducible code.
>>>
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>> ______________________________**________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/**listinfo/rhelp< https://stat.ethz.ch/mailman/listinfo/rhelp>
>> PLEASE do read the posting guide http://www.Rproject.org/**>> postingguide.html < http://www.Rproject.org/postingguide.html>
>> and provide commented, minimal, selfcontained, reproducible code.
>>
>
> David Winsemius, MD
> Alameda, CA, USA
>
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

