Re: extracting column data from 2D Matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg23428] Re: extracting column data from 2D Matrix
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 10 May 2000 02:32:14 -0400 (EDT)
- References: <8f54qe$487@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Chris,
Please enter the following into Mathematica and evaluate.
First question:
xyData = Table[Random[Integer, 9], {3}, {5}]
xyData[[All, {1, 3, 5}]]
Second question: first a step by step solution.
a0 = Rest[Transpose[xyData]]
a1 = Partition[a0, 2]
a2 = Map[Sequence @@ Append[#, (Plus @@ #)/Length[#]] &, a1, {1}]
a3 = Prepend[a2, xyData[[All, 1]]]
Transpose[a3]
Now put this together in a function with some input tests.
columnMeans[data_, n_Integer] /; IntegerQ[(Dimensions[data][[2]] - 1)/n
] :=
Transpose[
Prepend[Map[Sequence @@ Append[#, (Plus @@ #)/n] &,
Partition[Rest[Transpose[data]], n], {1}], data[[All, 1]]]]
Check the function for correctness
columnMeans[xyData, 2]
Time on a largish example.
tst = Table[Random[], {1000}, {101}];
(res = columnMeans[tst, 10]); // Timing
{1.26 Second, Null}
Dimensions[res]
{1000, 111}
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Krautschik, Chris G" <krautschikc at intel.co.jp> wrote in message
news:8f54qe$487 at smc.vnet.net...
> I have a 2D Matrix of say 101 columns and 1000 rows. Let's call the data
> matrix xyDATA. The first column represents the X coordinate and all other
> columns represent Y coordindates for different input conditions.
>
> Let's say I need to extract every 10th y column as well as the X column
(1st
> column) so I can plot the data in another program. I have been using the
> following expression for the extraction process:
>
> colmnIndex={#1, #11,#21,#31#41,#51,#61,#71,#81,#91,#101};
> xy10 = columIndex & @@@ xyDATA;
>
> How do I automate the process for the column Index without have to type it
> each time? That is perhaps something like the following I had in mind:
>
> yindex= #*Table[i,{1,101,10}];
> columnIndex2=Append[{#1},yindex];
>
> This though doesn't seem to work even when I used:
>
> ToExpression[columnIndex2]
>
> or tried various other string to expression schemes.
>
> Also I like to average each 10 consequitive rows. That is I have written a
> RowMean function that generate the column vector of the 1000 elements. How
> do I then insert after each 10 y column the columns that holds the
average?
> So the matrix now grows from 101 colums to 111 colums (still 1000 rows)
but
> the averaged columns need to be inserted at (12,22,33,...).
>
> thanks,
> Chris
>
>
>