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: Passing a subvector to a void function


From   Joe Canner <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: Passing a subvector to a void function
Date   Mon, 29 Jul 2013 13:31:26 +0000

Sergiy,

Thanks for looking into this.  Sorry, I should have simplified the example before posting.  The issue is not whether the subroutine will modify the original vector, the issue is whether you can pass a sub-vector and have it come back modified.  To use your suggested code to illustrate:

clear all
mata
void arrsum(real rowvector A, real k) {
real scalar S, i
  S=0
  for(i=1;i<=cols(A);i++) {
    S=S+A[1,i]
  }
  A[k]=S
}

A=1,2,3,4
A
arrsum(A[|2\.|],1)
A
end

So, instead of passing the entire vector A, I am passing a subvector starting with the 2nd element of A.  What I expect (and want) is for arrsum to put the sum of elements 2-4 into element 2 (since it arrsum is putting the sum of elements 1-3 of the subvector into element 1 of the subvector).  However, if you run this code you will find that A is unmodified upon return.

Regards,
Joe

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Sergiy Radyakin
Sent: Monday, July 29, 2013 2:56 AM
To: [email protected]
Subject: Re: st: Passing a subvector to a void function

Joe, you have too many irrelevant arguments in the procedure you are discussing, they complicate understanding. As to the subject you ask, consider the following example: it computes the sum of the array elements, then replaces the specified element of the array with the computed sum. The modified array is returned to the caller.
Sergiy

clear all
mata
void arrsum(real rowvector A, real k) {
  S=0
  for(i=1;i<=cols(A);i++) {
    S=S+A[1,i]
  }
  A[k]=S
}

A=1,2,3,4
A
arrsum(A,1)
A
end

On Fri, Jul 26, 2013 at 12:12 PM, Joe Canner <[email protected]> wrote:
> A question for all you Mata programmers:
>
> I am converting a Fortran 90 program to Mata and the Fortran program passes subarrays as arguments to subroutines like this:
>
>         call idtang ( ndp, xd, yd, nt, iwk(jwipt), nl, iwk(jwipl),  
> iwk(jwiwl), iwk(jwiwp), wk )
>
> where iwk is an array and iwk(jwipt) means "pass a subarray of iwk starting at element jwipt".  The subroutine modifies this subarray and passes it back.
>
> I converted this to Mata as follows:
>
>         idtang ( ndp, xd, yd, nt, iwk[|jwipt\.|], nl, iwk[|jwipl\.|], 
> iwk[|jwiwl\.|], iwk[|jwiwp\.|], wk )
>
> but iwk is empty upon return.
>
> Is there a way to do this that will return the modified subvector and put it back in iwk at position jwipt?
>
> I got it to work by creating a new vector ipt, setting it to iwk[|jwipt\.|] and passing it to the function, but I was hoping to avoid having to make so many substantive changes to the original code.
>
> Thanks for your help!
>
> Joe Canner
> Johns Hopkins University School of Medicine
>
> *
> *   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