Build matrices iteratively with functional constructs
- To: mathgroup at smc.vnet.net
- Subject: [mg105438] Build matrices iteratively with functional constructs
- From: Garapata <warsaw95826 at mypacks.net>
- Date: Wed, 2 Dec 2009 06:28:29 -0500 (EST)
I try to keep learning about functional programing constructs (with lots of help from reading through posts on this forum), but now I've gotten stuck as I've tried to apply such approaches to nested lists and matrices. I need to construct three matrices whose successive values depend on earlier values in matrices (i.e. earlier in a time series). This can get a bit confusing so let's start with as much as I've got working so far, then see if I can describe the problem clearly enough for someone to help. Initialize some variables: intervals = 10; measurements = 3; samples = 5; Build 2 nested lists: pList = Array[p, {intervals, measures}]; sList = Array[s, {measurements, samples}]; I typically look at them in MatrixForm: pList // MatrixForm sList // MatrixForm pList represents a time series of intervals with associated measurement values. In the field we'll take 5 samples these measurements during any time period. I create a ratio of each row of pList to its corresponding elements in sList with the following: spRatio = sList / # & /@ pList; spRatio // MatrixForm This gives me a nested list of the following dimensions: Dimensions[spRatio] {10, 3, 5} Now I need some help. I can do the following a step at a time, but I'd like a way to apply this all at once in a functional style for speed and simplicity. step 0: Initialize an vector w0 = Array[1, samples] w0 // MatrixForm Start a loop Step 1: The following takes a part of spRatio, the first row in the matrix: spRatio1 = spRatio[[1, 1 ;; 3, All]]; spRatio1 // MatrixForm Step 2: pos1 = Transpose[w0*Transpose[spRatio1]]; pos1 // MatrixForm Step 3a: comp1 = (pos1 . w0)* numOfSamples/Total[w0]; comp1 // MatrixForm Step 3b: w1 = pList[[1]].pos1; Repeat loop for interval - 1 steps incrementing some appropriate index (w0, w1, w2, ...; pos1, pos2, pos3, ...; comp1, comp2, comp3 ...; spRatio1 = spRatio[[1, 1 ;; 3, All]], spRatio2 = spRatio[[2, 1 ;; 3, All]] ). I could put this kind of thing in a Do loop and use Append to build the various matrices, but there must be a better way. At the end of this process I want 3 matrices with the following dimensions, let's designate them: matrix dimensions ----------------------------------- w {5, 10} pos {9, 5, 3} comp {9, 3} The 9s corresponding to intervals -1. I don't need to preserve w and pos I really only need comp as output so that may affect the efficiency of a solution. I've tried to figure out ways of using Map, MapThread, Fold, and FoldList to do this, but haven't made headway. The specific calculations of each the above steps aside, I really need a way build nested lists and matrices from earlier parts of one or more matrices. Thanks to all.