Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: nested loop in mata
From
"Frederick Guy" <[email protected]>
To
<[email protected]>
Subject
st: nested loop in mata
Date
Sat, 26 Feb 2011 18:32:01 -0000
Dear Statalisters,
I have I values certain variables, J values for certain other ones, I>J. For each i, I need to do a certain operation with every j, returning one value of the result for each i, so I should have I results. I've run the code below. I do get I values, but after the first J all are zero so it seems I must be structuring the loops wrong. Any suggestions would be appreciated.
Frederick
################################################################################
mata
mata clear
x = st_data(.,("mergeidf", "lat_r_p", "lon_r_p", "dis_id", "mergeidu", "lat_r_u", "lon_r_u", "dw_1", /*
*/ "dw_2", "dw_3", "dw_4", "dw_5", "dw_6", "dw_7", "dw_8", "dw_9", "dw_10", "p_count", "u_count"))
fobs = x[1,18]
uobs = x[1,19]
fobs
uobs
distances = J(fobs,uobs,0)
d_inverse = J(fobs,uobs,0)
d_related = J(fobs,uobs,0)
for (i=1; i<=fobs; i++) {
for (j=1; j<=uobs; j++) {
[various operations, creating d, dinv, and drel]
distances[i,j]=d
d_inverse[i,j]=dinv
d_related[i,j]=drel
}
}
dist_mean=colsum(distances)/(rows(distances)-1)
d_p_sum = colsum(d_inverse)
d_rel_sum = colsum(d_related)
dist_mean
d_p_sum
d_rel_sum
st_addvar("float","di_ur")
st_store(.,"di_ur", d_rel_sum')
st_addvar("float","di_u")
st_store(.,"di_u", d_p_sum')
end // leaving Mata
clear mata
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/