Re: Dealing with irregular shaped arrays
- To: mathgroup at smc.vnet.net
- Subject: [mg22063] Re: Dealing with irregular shaped arrays
- From: "Mariusz Jankowski" <mjkcc at usm.maine.edu>
- Date: Fri, 11 Feb 2000 02:38:49 -0500 (EST)
- Organization: University of Southern Maine
- References: <87trbn$5ns@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Steve, First a comment: upgrade to version 4 since its functionality for
reading data and manipulating arrays is much improved. However, in version
3, the following will work.
In[2]:=
strm = OpenRead["d:\\temp\\test.txt"]
Out[2]=
InputStream["d:\\mjkcc\\temp\\test.txt", 5]
In[4]:=
data = ReadList[strm, Number, RecordLists -> True]
Out[4]=
{{1, 3, 5}, {2, 2, 1, 3, 7}, {3, 9}, {4, 7, 9, 2},
{5, 2, 1}}
Define a padding function. Version 4 has a built-in function.
In[5]:=
pad[x_, L_] := Join[x, Table["na", {L - Length[x]}]]
Find the length of the longest record
In[6]:=
Max[Length /@ data]
Out[6]=
5
Pad each record and transpose
In[9]:=
arr=Transpose[(pad[#1, %] & ) /@ data]
Out[9]=
{{1, 2, 3, 4, 5}, {3, 2, 9, 7, 2}, {5, 1, "na", 9, 1},
{"na", 3, "na", 2, "na"}, {"na", 7, "na", "na", "na"}}
At this point you are done.
A1=arr[[1]];
A2=arr[[2]]; etc
Good luck,
Mariusz
======================================
Mariusz Jankowski
University of Southern Maine
email: mjkcc at usm.maine.edu
Steve <nospam at nospam.com> wrote in message news:87trbn$5ns at smc.vnet.net...
> I need to import and process an irregular shaped array contained in a
> tab-delimited text file using version 3.0. The simplified structure
> looks like the following 5 rows of data except that in general, the
> actual data will not necessarily be integer:
>
> 1 3 5
> 2 2 1 3 7
> 3 9
> 4 7 9 2
> 5 2 1
>
> I need to "pad" the empty slots with a text string such as "N/A" so that
> the resulting array will be a complete 5x5 array. Then I need to assign
> a unique variable name to each column so that the end result will look
> like:
>
> A={1,2,3,4,5}
> B={3,2,9,7,2}
> C={5,1,N/A,9,1}
> D={N/A,3,N/A,2,N/A}
> E={N/A,7,N/A,N/A,N/A}
>
> I've tried
>
> in1=OpenRead["Input.txt"]
> data=ReadList[in1,Number,RecordLists->True]//TableForm
>
> which returns the array with the "empty slots" but I don't see how to
> partition this into the structure I need.
>
> Thanks for any help.
>
> Steve
>
>