# Replace element of matrix based on rule which invloves other element of same matrix

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"}
}


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]


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 *)

• 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 Apr 25, 2015 at 6:27
• 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] ] Apr 25, 2015 at 6:28
• optionprice[100, 10, 1, 0.2, 0.01] gives following output where last 1st row of last column is 1 Apr 25, 2015 at 6:30
• \!( 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"} }, Apr 25, 2015 at 6:31
• Anyway I can use Reverse@FoldList on most updated version of matrix fd please? Apr 25, 2015 at 6:32