0
$\begingroup$

I am trying to replace 1s row of matrix d iteratively from right by multiplying last element in 1st row with (1-int*dt) to generate 2nd last element. The replacement continues till I reach the 1st element of the row using code below. Can anybody help to let me know where I am going wrong. The code doesnt give error but gives out initial element without doing replacement. I am trying to find out a efficient way to replace elements of the matrix recursively based on certain rules/function. Is ReplacePart best to use or there is other way?

elemOperate[int_, dt_, nas_, nts_] := 
 Module[{d},
  d =  ConstantArray[2, {nas + 1, nts}];
  For[j = nts, j--, 
   d = 
    ReplacePart[d, {1, j_ - 1} :> (1 - int*dt)*d[[1, j]]]
  ];
  Grid[d]
 ]

elemOperate[0.01, 0.1, 4, 4] 

generates following output:

{
   {"2", "2", "2", "2"},
   {"2", "2", "2", "2"},
   {"2", "2", "2", "2"},
   {"2", "2", "2", "2"},
   {"2", "2", "2", "2"}
  }
$\endgroup$

1 Answer 1

1
$\begingroup$

First, a fixed version of your code:

ClearAll[eOF0, eOF1, eOF2, eOF3]
eOF0[int_, dt_, nas_, nts_] := 
 Module[{d = ConstantArray[2, {nas + 1, nts}]}, 
  For[j = nts, j > 1, j--, d = ReplacePart[d, {1, j - 1} -> (1 - int*dt)*d[[1, j]]]];
  Grid[d]]
eOF0[0.01, 0.1, 4, 4] 

enter image description here

and a few alternatives

eOF1[int_, dt_, nas_, nts_] := Module[{d = ConstantArray[2, {nas + 1, nts}]}, 
  For[j = nts, j > 1, j--, d[[1, j - 1]] = (1 - int*dt)*d[[1, j]]];   Grid[d]]

eOF2[int_, dt_, nas_, nts_] := Module[{d = ConstantArray[2, {nas + 1, nts}]},
  d[[1]] = Reverse@FoldList[(1 - int dt) # &, d[[1]]]; Grid@d]

eOF3[int_, dt_, nas_, nts_] := Module[{d = ConstantArray[2, {nas + 1, nts}]},
  Table[d[[1, j - 1]] = (1 - int*dt)*d[[1, j]], {j, nts, 2, -1}]; Grid[d]]

They give the same result as eOF0:

eOF1[0.01, 0.1, 4, 4] == eOF2[0.01, 0.1, 4, 4] == 
     eOF3[0.01, 0.1, 4, 4]== eOF0[0.01, 0.1, 4, 4]
(* True *)
$\endgroup$
6
  • $\begingroup$ Thanks kguler. I like e0F2. However when I am trying to use e0F2 as a in code where I have done matrix operation on same matrix, Revese@FoldList is ignoring intermediate steps and just working on the intial array. pl refer to code below $\endgroup$
    – Kausik
    Apr 25, 2015 at 6:27
  • $\begingroup$ optionprice[strike1_, nas1_, expn1_, vol1_, int_] := Module[{ds, s, dt, dt1, t, nts, fd}, ds = 2*strike1/nas1; dt = 0.9/(vol1*vol1*nas1* nas1) ; nts = IntegerPart [expn1 /dt] + 1; dt1 = expn1/nts; s = Table[ids, {i, 0, nas1 + 1}]; t = Table [jdt1, {j, 0, nts}]; fd = ConstantArray[1, {nas1 + 1, nts}]; fd = ReplacePart[fd, {i_, nts} :> Max[(s[[i]] - strike1), 0]]; fd[[1]] = Reverse@FoldList[(1 - int *dt1) # &, fd[[1]]]; Grid[fd] ] $\endgroup$
    – Kausik
    Apr 25, 2015 at 6:28
  • $\begingroup$ optionprice[100, 10, 1, 0.2, 0.01] gives following output where last 1st row of last column is 1 $\endgroup$
    – Kausik
    Apr 25, 2015 at 6:30
  • $\begingroup$ \!( TagBox[GridBox[{ {"0.992023968016", "0.994011992", "0.996004", "0.998", "1"}, {"1", "1", "1", "1", "0"}, {"1", "1", "1", "1", "0"}, {"1", "1", "1", "1", "0"}, {"1", "1", "1", "1", "0"}, {"1", "1", "1", "1", "0"}, {"1", "1", "1", "1", "20"}, {"1", "1", "1", "1", "40"}, {"1", "1", "1", "1", "60"}, {"1", "1", "1", "1", "80"}, {"1", "1", "1", "1", "100"} }, $\endgroup$
    – Kausik
    Apr 25, 2015 at 6:31
  • $\begingroup$ Anyway I can use Reverse@FoldList on most updated version of matrix fd please? $\endgroup$
    – Kausik
    Apr 25, 2015 at 6:32

Your Answer

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

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