MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: InputStream in version 3 vs version 4

  • To: mathgroup at smc.vnet.net
  • Subject: [mg18824] Re: [mg18773] InputStream in version 3 vs version 4
  • From: "Tomas Garza" <tgarza at mail.internet.com.mx>
  • Date: Thu, 22 Jul 1999 08:19:36 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

ELLIS, Luci [EllisL at rba.gov.au] wrote:

> Hi MathGroupers,
> I am trying to read in a standard tab-delimited text file of
> data. It works
> under version 3, but not version 4, and I have no idea why.  I
> tried setting
> the DOSTextFormat option to False, and that didn't help. There
> didn't appear
> to be anything in the manual indicating that this function had changed in
> version 4. Does anyone have any suggestions? If it helps, I'm using NT.
<snip>

Hi Luci,

If your file is a text file, then you had better read it as such, and then
convert to numbers those elements which are meant to be numbers for further
manipulation. I have your sample data in an Excel file called lp1.xls.
First, I convert it to a tab-delimited text file, and read it with

In[1]:=
a = ReadList["c:\Mis documentos\lp1.txt", Word, RecordLists -> True]
Out[1]=
{{"Series", "GDP", "NFGDP", "GDPDEF", "NFGDPDEF"}, {"Sep-89", "108379",
    "105023", "86.5", "85.5"}, {"Dec-89", "108768", "105231", "86.8",
    "86"}, {"Mar-90", "109335", "105769", "88.7", "88.1"}, {"Jun-90",
    "109885", "106371", "89.2", "88.7"}, {"Sep-90", "109241", "105519",
    "90.2", "90.4"}, {"Dec-90", "109339", "105552", "90.7",
    "91.1"}, {"Mar-91", "108692", "105250", "91.2", "91.6"}}

The numeric data (which I suppose you want to use as numbers) are then

In[2]:=
b = ToExpression /@ Drop[#, 1] & /@ Drop[a, 1]
Out[2]=
{{108379, 105023, 86.5, 85.5}, {108768, 105231, 86.8, 86}, {109335, 105769,
    88.7, 88.1}, {109885, 106371, 89.2, 88.7}, {109241, 105519, 90.2,
    90.4}, {109339, 105552, 90.7, 91.1}, {108692, 105250, 91.2, 91.6}}

and if you wish to have your matrix with headers and first columns as text,
then

In[3]:=
Prepend[Transpose[Prepend[Transpose[b], Rest[Transpose[a][[1]]]]], a[[1]]]
Out[3]=
{{"Series", "GDP", "NFGDP", "GDPDEF", "NFGDPDEF"}, {"Sep-89", 108379,
105023,
    86.5, 85.5}, {"Dec-89", 108768, 105231, 86.8, 86}, {"Mar-90", 109335,
    105769, 88.7, 88.1}, {"Jun-90", 109885, 106371, 89.2, 88.7}, {"Sep-90",
    109241, 105519, 90.2, 90.4}, {"Dec-90", 109339, 105552, 90.7,
    91.1}, {"Mar-91", 108692, 105250, 91.2, 91.6}}

will do the job.

> Failing this, can anyone suggest a robust way to turn a list of functions
> and a matrix of data into a matrix where the elements of each row are the
> elements of the original matrix transformed by the corresponding function?

Suppose you have four functions f1, f2, f3, f4:

In[4]:=
vec = {f1, f2, f3, f4}
Out[4]=
{f1, f2, f3, f4}

Then

In[5]:=
c = MapThread[List, {vec, #}] & /@ b
Out[5]=
{{{f1, 108379}, {f2, 105023}, {f3, 86.5}, {f4, 85.5}}, {{f1, 108768}, {f2,
      105231}, {f3, 86.8}, {f4, 86}}, {{f1, 109335}, {f2, 105769}, {f3,
      88.7}, {f4, 88.1}}, {{f1, 109885}, {f2, 106371}, {f3, 89.2}, {f4,
      88.7}}, {{f1, 109241}, {f2, 105519}, {f3, 90.2}, {f4, 90.4}}, {{f1,
      109339}, {f2, 105552}, {f3, 90.7}, {f4, 91.1}}, {{f1, 108692}, {f2,
      105250}, {f3, 91.2}, {f4, 91.6}}}

If now you define

In[6]:=
trans[{x1_, y1_}] := Map[x1, {y1}]

then the following gets what you wanted:

In[7]:=
Map[trans, c, {2}]
Out[7]=
{{{f1[108379]}, {f2[105023]}, {f3[86.5]}, {f4[85.5]}}, {{f1[108768]}, {f2[
        105231]}, {f3[86.8]}, {f4[86]}}, {{f1[109335]}, {f2[105769]}, {f3[
        88.7]}, {f4[88.1]}}, {{f1[109885]}, {f2[106371]}, {f3[89.2]}, {f4[
        88.7]}}, {{f1[109241]}, {f2[105519]}, {f3[90.2]}, {f4[90.4]}}, {{f1[
        109339]}, {f2[105552]}, {f3[90.7]}, {f4[91.1]}}, {{f1[108692]}, {f2[
        105250]}, {f3[91.2]}, {f4[91.6]}}}


Hope this helps,

Tomas Garza
Mexico City



  • Prev by Date: RE: Re: Is there a FAQ? (Clear all)
  • Next by Date: RE: Re: NSolve with varying parameter
  • Previous by thread: Re: InputStream in version 3 vs version 4
  • Next by thread: Re: InputStream in version 3 vs version 4