Hi
start simple!
Work out *each* row combined with *each* row,
to give (in your case) a 26-by-26 matrix.
Only after you have got this working, start thinking about
making it run faster [eg by
only evaluating the upper triangular entries]
To do a nested loop, do
M <- matrix(0,n,n)
for(i in seq_len(n)){
for(j in seq_len(n)){
M[i,j] <- f(i,j)
}
}
which will fill in matrix M for you.
HTH
rksh
Gerard M. Keogh wrote:> Hi all,
>
> apologies if this is obvious - but I can't see it and would appreciate
some
> quick help!
>
> the matrix mhouse is 26x3 and I'm computing odds ratios. The simple
code
> below "should" compute the odds vector for every pair (325) i.e.
26C2 in
> cols 1 and 2.
> On the first i=1 outer loop the inner j loop runs from 2 to 26 ok and then
> I get the error (Error: subscript out of bounds)
>
> Why isn't my loop incrementing i - the outer loop to 2 and then
resetting
> j=3?
> Am I missing something obvious?
>
> thanks
>
> Gerard
>
>
> > mhouse
> [,1] [,2] [,3]
> [1,] 275 819 49
> [2,] 593 1323 192
> [3,] 813 1181 292
> [4,] 2177 5189 1320
> [5,] 1651 2243 270
> [6,] 1061 5629 11035
> [7,] 1690 2302 589
> [8,] 1130 1203 345
> [9,] 565 1898 655
> [10,] 580 730 234
> [11,] 343 1761 73
> [12,] 372 536 67
> [13,] 666 1713 397
> [14,] 382 918 279
> [15,] 486 921 247
> [16,] 1141 988 313
> [17,] 626 1135 666
> [18,] 438 436 168
> [19,] 425 691 101
> [20,] 609 716 99
> [21,] 467 661 141
> [22,] 879 1373 79
> [23,] 444 1101 130
> [24,] 459 898 351
> [25,] 995 1801 398
> [26,] 396 1107 201
> > # set up the odds vector by declaring it to be null
> > odds=NULL
> >
> > # compute the odds ratios for Individual House vs Scheme
House
> > for (i in 1:25)
> + {
> + for (j in i+1:26)
> + {
> + todds = (mhouse[i,1]*mhouse[j,2])/(mhouse[j,1]*mhouse[i,2])
> + # compute the todds for row i with row j:
> j>i
> + odds = c(odds,todds)
> + # append todds to the odds vector
> + }
> + }
> Error: subscript out of bounds
> > odds
> [1] 0.7491244 0.4877622 0.8003391 0.4561745 1.7814132 0.4573697
> 0.3574670 1.1279674 0.4226138 1.7239078 0.4838053
> [12] 0.8636384 0.8069156 0.6363150 0.2907502 0.6087939 0.3342421
> 0.5459312 0.3947703 0.4752623 0.5244818 0.8326321
> [23] 0.6569199 0.6077702 0.9386447
> >
>
>
>
**********************************************************************************
> The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged material.
Any review, retransmission, dissemination or other use of, or taking of any
action in reliance upon, this information by persons or entities other than the
intended recipient is prohibited. If you received this in error, please contact
the sender and delete the material from any computer. It is the policy of the
Department of Justice, Equality and Law Reform and the Agencies and Offices
using its IT services to disallow the sending of offensive material.
> Should you consider that the material contained in this message is
offensive you should contact the sender immediately and also
mailminder[at]justice.ie.
>
> Is le haghaidh an duine n? an eintitis ar a bhfuil s? d?rithe, agus le
haghaidh an duine n? an eintitis sin amh?in, a bhearta?tear an fhaisn?is a
tarchuireadh agus f?adfaidh s? go bhfuil ?bhar faoi r?n agus/n? faoi phribhl?id
inti. Toirmisctear aon athbhreithni?, atarchur n? leathadh a dh?anamh ar an
bhfaisn?is seo, aon ?s?id eile a bhaint aisti n? aon ghn?omh a dh?anamh ar a
hiontaoibh, ag daoine n? ag eintitis seachas an faighteoir beartaithe. M? fuair
t? ? seo tr? dhearmad, t?igh i dteagmh?il leis an seolt?ir, le do thoil, agus
scrios an t-?bhar as aon r?omhaire. Is ? beartas na Roinne Dl? agus Cirt,
Comhionannais agus Athch?irithe Dl?, agus na nOif?g? agus na nGn?omhaireachta? a
?s?ideann seirbh?s? TF na Roinne, seoladh ?bhair chol?il a dh?chead?.
> M?s rud ? go measann t? gur ?bhar col?il at? san ?bhar at? sa
teachtaireacht seo is ceart duit dul i dteagmh?il leis an seolt?ir l?ithreach
agus le mailminder[ag]justice.ie chomh maith.
>
***********************************************************************************
>
>
>
> ______________________________________________
> R-help at 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.
>
--
Robin K. S. Hankin
Uncertainty Analyst
University of Cambridge
19 Silver Street
Cambridge CB3 9EP
01223-764877