Bookmark and Share

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]

RE: st: How to save a series of vector from a double loop in mata


From   Preeya Mohan <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: How to save a series of vector from a double loop in mata
Date   Tue, 23 Jul 2013 07:53:08 -0400

Thank you so very much Sergiy. What you suggested worked!!

Preeya
----------------------------------------
> Date: Mon, 22 Jul 2013 21:26:29 -0400
> Subject: Re: st: How to save a series of vector from a double loop in mata
> From: [email protected]
> To: [email protected]
>
> Preeya, not sure what your program is doing wrong, but here is an
> example for Euclidian distance. Simply adjust the formula within the
> loop for your case. If this does not help, please let me/us see your
> code. Sergiy
>
> clear all
>
> mata
>
> A=10,12\14,23\10,8\5,39
> B=20,12\20,23\11,5\5,17
>
> assert(rows(A)==rows(B))
>
> D=J(16,1,.)
> for(i=1;i<=4;i++) {
> for(j=1;j<=4;j++) {
> D[(i-1)*4+j,1]=sqrt( (A[i,1]-B[j,1])^2 + (A[i,2]-B[j,2])^2)
> }
> }
>
> D
>
> end
>
> 1
> +---------------+
> 1 | 10 |
> 2 | 14.86606875 |
> 3 | 7.071067812 |
> 4 | 7.071067812 |
> 5 | 12.52996409 |
> 6 | 6 |
> 7 | 18.24828759 |
> 8 | 10.81665383 |
> 9 | 10.77032961 |
> 10 | 18.02775638 |
> 11 | 3.16227766 |
> 12 | 10.29563014 |
> 13 | 30.88689042 |
> 14 | 21.9317122 |
> 15 | 34.525353 |
> 16 | 22 |
> +---------------+
>
> On Mon, Jul 22, 2013 at 8:29 PM, Preeya Mohan
> <[email protected]> wrote:
>> Dear Statalisters,
>>
>> I am having a problem saving a series of vectors from a double loop using mata. I am working with Stata 12.1 for Windows.
>>
>> I calculate distance between two locations using the haversine formula from a double loop in mata. Let's call the two locations location A and B. I create two matrices A and B each containing the latitude and longitude coordinates. For each A location, I calculate out the distance to each B location, that is, if each A and B matrix contain 4 rows each in the end I should get 16 distances calculated as shown below. Each time the loop runs however 4 column vectors are created containing only 4 distances each and I can only save the last vector. I would however like to store each vector into one large matrix with all 16 distances.
>>
>> Thank you for your time in reading this.
>>
>> A B distance
>> 1 1
>> 1 2
>> 1 3
>> 1 4
>> 2 1
>> 2 2
>> 2 3
>> 2 4
>> 3 1
>> 3 2
>> 3 3
>> 3 4
>> 4 1
>> 4 2
>> 4 3
>> 4 4
>>
>>
>> Yours,
>> Preeya
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>> * http://www.ats.ucla.edu/stat/stata/
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/ 		 	   		  
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index