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   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Passing a subvector to a void function
Date   Mon, 29 Jul 2013 02:56:25 -0400

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/


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