0

I have the following function:

vt[ea_, v0_, cm_, b_] := ea + (v0 - cm)*b

ea is a set of lists and cm is also set of lists, b is a constant and for v0 the startvalue is 5000. After the first step the function should use the output of the expression vt[ea_, v0_, cm_, b_] for v0. It is a recursion function. For this I can use the following FoldList definition:

FoldList[vt[#2[[1]], #1, #2[[2]], b] &, v0, {eacm}] 

For ea and cm, I have a set of lists a little bit more than in the example below.

ea = {{5, 6, 7}, {1, 2, 3}, {4, 8, 9}} 
cm = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Now I transposed this into a list of pairs with the following expression to use it in the FoldList command:

Transpose[{ea, cm}, {3, 1, 2}]

The output is the following multidimensional array:

eacm = 
  {{{5, 1}, {6, 2}, {7, 3}}, {{1, 4}, {2, 5}, {3, 6}}, {{4, 7}, {8, 8}, {9, 9}}}

But the FoldList expression above doesn´t work with this multidimensional array. It only works with one list. I think I have to change something in the #2[[1]], #2[[2]] arguments.

My first question is: what do I have to change in the FoldList expression above to get it to work? My second question is: can I transform the multidimensional array into a matrix or a set of lists with the form:

eacm = 
 {{5, 1}, {6, 2}, {7, 3}}, {{1, 4}, {2, 5}, {3, 6}}, {{4, 7}, {8, 8}, {9, 9}}

I think I am close to a solution of my problem. Hope someone can help me.

LCarvalho
  • 9,233
  • 4
  • 40
  • 96
user41673
  • 133
  • 6
  • The second question is impossible, that's not a list. A list must always be enclosed by curly brackets. You can run eacm[[i]] for {1,2,3} to make three seperate lists – Feyre Jul 17 '16 at 11:06
  • Ok thank you but that will be nearly immpossible because i will have like 10.000 set of lists or is there a fast way to automaticly get all the lists seperated? (I mean not to put in 1 and than 2 and so on?) – user41673 Jul 17 '16 at 11:12
  • 3
    I think we're dealing with an XY problem here. Why not step back and discuss what your actual problem is first before we start discussing solutions? – J. M.'s missing motivation Jul 17 '16 at 13:01
  • This may be a duplicate of (39476) – Mr.Wizard Jul 17 '16 at 14:18

1 Answers1

0
vt[ea_, v0_, cm_, b_] := ea + (v0 - cm)*b

v0 = 3500;
b = 1.1;

ea = {{5, 6, 7}, {1, 2, 3}, {4, 8, 9}};
cm = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

The manner in which you created eacm is fine.

eacm = Transpose[{ea, cm}, {3, 1, 2}]

(* {{{5, 1}, {6, 2}, {7, 3}}, {{1, 4}, {2, 5}, {3, 6}},
    {{4, 7}, {8, 8}, {9, 9}}} *)

The change to the FoldList code is to wrap it in Map in order to apply it to the individual sub-lists in eacm.

Map[FoldList[vt[#2[[1]], #1, #2[[2]], b] &,
   v0, eacm[[#]]] &, Range[Length@eacm]]

{{500, 504, 508, 512}, {500, 497, 494, 491}, {500, 497, 497, 497}}

Mr. Wizard showed an improvement that directly uses eacm rather than Range[Length@eacm].

Map[FoldList[vt[#2[[1]], #1, #2[[2]], b] &, v0, #] &, eacm]

or using a different syntax

FoldList[vt[#2[[1]], #1, #2[[2]], b] &, v0, #] & /@ eacm
Jack LaVigne
  • 14,462
  • 2
  • 25
  • 37
  • Yes i already tried this but i want the FoldList command to stop after one List: {{5, 1}, {6, 2}, {7, 3}}, and her begin from new with the computation {{1, 4}, {2, 5}, {3, 6}},and here again {{4, 7}, {8, 8}, {9, 9}} so i think i need somethin like this to hold the pair of lists in seperate lists: – user41673 Jul 17 '16 at 12:06
  • eacm = {{5, 1}, {6, 2}, {7, 3}}, {{1, 4}, {2, 5}, {3, 6}}, {{4, 7}, {8, 8}, {9, 9}} – user41673 Jul 17 '16 at 12:07
  • This is what i was searching for thank you very much Jack LaVigne – user41673 Jul 17 '16 at 16:42
  • I have adjusted the description of the problem. I hope now it is a little bit easier to understand. Again thanks to Jack LaVigne for this great solution!! – user41673 Jul 17 '16 at 17:05
  • 1
    Why would one not use FoldList[vt[#2[[1]], #1, #2[[2]], b] &, v0, #] & /@ eacm? – Mr.Wizard Jul 17 '16 at 17:45
  • @Wizard They are identical – Jack LaVigne Jul 17 '16 at 19:52
  • @Wizard Sorry, I mis-read it. I thought you were talking about the difference between using Map[... and /@ eacm. But you meant using eacm[[#]] and Range as opposed to simply using eacm. Your suggestion is better and I have incorporated it. – Jack LaVigne Jul 17 '16 at 21:16
  • @Jack LaVigne, Many users with expert Mma proficiency use expr[[1]] and expr[[2]] when First@expr and Last@expr will do the same thing. The code from you and Mr. Wizard is an example. The former takes a bit more time because Part has to evaluate the 1 or 2 to determine which part. Also, I find the later more readable (ie. easier for my eyes to parse). Hence I can't understand why we don't see more use of First & Last. – Ted Ersek Jul 17 '16 at 23:27
  • @Ted Ersek I agree. – Jack LaVigne Jul 17 '16 at 23:28