Dumb Question About Variables (Revisited) Plus Matrices, Good Golly

Gregory Lypny gregory.lypny at videotron.ca
Wed Mar 27 10:25:01 EST 2002


     Thanks, Raymond, Pierre, and others for responding to my question.

     Both Raymond (quoted below) and Pierre Sahores suggest the use of 
arrays (vectors) for my problem.  That was my first inclination, but I 
ran into a bit of a glitch when I tried to create vectors out of the 
columns of a matrix.  I don't want to be long-winded here, but maybe my 
problem is of interest to others, so here goes.

     I'm developing a simple statistics stack.  It reads in a 
tab-delimited text file of numbers that has N rows of records and K 
columns of variables.  My sample data is N=1,000 and K=10, so each column 
contains 1,000 observations for a variable.

     The first thing that my stack does is load the data into an NxK 
matrix (array) called X with each element indexed by (i,j), so X[3,4] 
becomes the third observation for the fourth variable.  Matrix X will be 
useful for computing covariance and correlation matrices as well as 
simple linear regressions (line fitting).  But it will also be important 
to be able to transform or do computations on each column (i.e., 
variable) separately, for example, to compute the average of variable 7.  
Apparently, MC doesn't have a command or function to extract or refer to 
individual columns of numerically indexed arrays (e.g., pull out column 4 
by referring to X[*,4]), so I just figured I'd create a separate array 
(or vector) for each variable.  That can be done easily in MC by 
transposing X and dumping each row into a separate single-indexed array 
called v like this:

put tranpose(x) into XT  -- KxN matrix, where each row has N=1,000 
observations
  repeat with j=1 to N
    repeat with i= 1 to K
      put XT[j,i] & "," after v[i]
      -- v[1] is a comma-delimited list of N observations on variable 1
      -- v[2] is a comma-delimited list of N observation on variable 2
      -- and so on
    end repeat
    delete last character of v[i]  -- get rid of the trailing comma
  end repeat

This appears to solve the variable-naming problem that Raymond and Pierre 
(and others) have kindly be helping me with because now I have K separate 
vectors (v[1], v[2], ... , v[K]) within an umbrella array called v.  And 
this is fine because most statistics can be computed using 
comma-delimited lists as input, but, unfortunately, I can't do matrix 
math on these lists.  It would be elegant (for what it's worth) and 
useful if each element of v referred to a single-index associative array, 
so that v[1] = (d[1], d[2],..., d[N]) for variable 1 and likewise for the 
other variables.  MC can do arrays within arrays (very, very cool), but 
wouldn't I get into a naming problem again because the elements in v[1] 
are named as d, and so would the elements of v[2].   That's what I'm 
tinkering with now.

     Greg


On 26/3/2002 8:33 PM, Raymond E. Griffith wrote: 

>Hmmm. Here is where an array structure would really shine.
>
>repeat with i = 1 to 10
> put i+10 into H[i]
>end repeat
>
>It is easy to use, and avoids the problems you note.
>
>Cheers!
>
>Raymond


>on Tue, 26 Mar 2002 10:33:43 Gregory Lypny <gregory.lypny at videotron.ca>
>wrote:
>
>> 
>> Hi Everyone,
>> 
>> A few days ago I asked about creating variables on the fly.  It was
>> suggested that I enclose the unchanging part of the variable name in
>> quotations.  So I tried the repeat loop below as an experiment, hoping to
>> generate H1 = 11, H2, =12, and so on, up to H10 =20.
>> 
>> repeat with i = 1 to 10
>> put i+10 into ("H" & i)
>> end repeat
>> 
>



More information about the metacard mailing list