Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Joseph Coveney" <jcoveney@bigplanet.com> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: doubt on Mata using multiple loops |
Date | Sun, 14 Mar 2010 22:48:12 +0900 |
A matrix of (mutual) connections ought to be symmetric. That and a couple of other things that dawned on me are cleaned up below. If your firm information is strings (firm names) instead of numbers, then you could either -encode- or substitute _st_sdata() and st_sdata() in the inner loop. Joseph Coveney version 11.0 clear * set more off input byte firm_nr byte connection1 byte connection2 1 5 3 2 0 0 3 1 0 4 2 0 5 1 0 end list , noobs abbreviate(15) separator(0) mata mata set matastrict on real matrix function getConnectedFirms(| real scalar show_duplicates) { show_duplicates = (args() == 0) ? 1 : show_duplicates // Opt-out real scalar observation_tally observation_tally = st_nobs() real matrix Results Results = J(observation_tally, observation_tally, 0) real scalar connection_nr, firm_nr // Indexes for (connection_nr=2; connection_nr<=st_nvar(); connection_nr++) { for (firm_nr=1; firm_nr<=observation_tally; firm_nr++) { Results[firm_nr, .] = Results[firm_nr, .] + (_st_data(firm_nr, 1) :== st_data(., connection_nr)') } } Results = Results + Results' if (show_duplicates) { return(Results) } else { return(Results :> 0) } } getConnectedFirms() getConnectedFirms(0) getConnectedFirms(1) end exit * * 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/