0
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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 $\endgroup$
    – Feyre
    Jul 17, 2016 at 11:06
  • $\begingroup$ 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?) $\endgroup$
    – user41673
    Jul 17, 2016 at 11:12
  • 3
    $\begingroup$ 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? $\endgroup$ Jul 17, 2016 at 13:01
  • $\begingroup$ This may be a duplicate of (39476) $\endgroup$
    – Mr.Wizard
    Jul 17, 2016 at 14:18

1 Answer 1

0
$\begingroup$
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
$\endgroup$
9
  • $\begingroup$ 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: $\endgroup$
    – user41673
    Jul 17, 2016 at 12:06
  • $\begingroup$ eacm = {{5, 1}, {6, 2}, {7, 3}}, {{1, 4}, {2, 5}, {3, 6}}, {{4, 7}, {8, 8}, {9, 9}} $\endgroup$
    – user41673
    Jul 17, 2016 at 12:07
  • $\begingroup$ This is what i was searching for thank you very much Jack LaVigne $\endgroup$
    – user41673
    Jul 17, 2016 at 16:42
  • $\begingroup$ 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!! $\endgroup$
    – user41673
    Jul 17, 2016 at 17:05
  • 1
    $\begingroup$ Why would one not use FoldList[vt[#2[[1]], #1, #2[[2]], b] &, v0, #] & /@ eacm? $\endgroup$
    – Mr.Wizard
    Jul 17, 2016 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.