# Finding a column from Max comparing each element of two columns; subsequent column used for backward calculation

I have

lensp=21; rho=0.975; e1=0; pid= 0.5212; qid=0.4788; tid=10;


based on my own calculations I have given vectors of dimension lenspx1

prf1[stsp]={2021.8180858663386, 2011.9930631686764, 1999.9243976405476,
1985.0997308573862, 1966.88970219104, 1944.5212301788522,
1917.0446924244275, 1883.2936106983718, 1841.8351297259853,
1790.9091873076468, 1728.3537933238854, 1651.5132454491252,
1557.125384999776, 1441.1831065203673, 1298.7642416896465,
1123.8225955042703, 908.9312634716356, 644.9673326999568,
320.7245813085048, -77.56226615034835, -566.8019243322452}

enpvc={22721.10462499856, 22610.691442815092, 22475.064299074696,
22308.46533185524, 22103.821813506762, 21852.44588782723,
21543.665739200012, 21164.372535491235, 20698.463910797407,
20126.160361876526, 19423.1655368233, 18559.6347672108,
17498.90805416407, 16195.953719050109, 14595.456646136767,
12629.469956219407, 10214.53041628219, 7248.115123919593,
3604.289039979013, -871.6414085290527, -6369.695629112519};

tenpvc = TableForm[enpvc];

enpva={1729.1679511432503, 1632.8636451934199, 1514.5672815927992,
1369.2567526075109, 1190.763101650733, 971.5086288031089,
702.1851900925085, 371.3590331761461, -35.01460672697431,
-534.1877578084313, -1147.3521225541263, -1900.5387420256466,
-2825.7231075362233, -3962.1826357017644, -5358.164136584017,
-7072.932064968695, -9179.284510440477, -11766.643739117186,
-14944.852491698064, -18848.837204384356, -23644.336123236586};


I am then creating a matrix of zeros and replacing the elements of the matrix as

newp = Table[e1, {i, lensp}, {j, lensp}];

Table[newp[[i, Min[i + 1, lensp]]] = pid, {i, lensp}];
Table[newp[[i, Max[i - 1, 1]]] = qid, {i, lensp}];


I am then creating another matrix "val" and replacing its last column with the vector enpvc

val = Table[e1, {i, lensp}, {j, tid + 1}];

val[[All, tid + 1]] = tenpvc;


The goal of the problem is, starting backwards, you take the Max of (enpva, prf1[stsp]+ rho* newp.val(last column)). You are comparing element for element between the 2 vectors and taking the max to get the second-to-last column of "val". Then you do Max of (enpva, prf1[stsp]+ rho* newp.val(second-to-last column)) and get the previous column and so on. You keep replacing until you get the first column of val.

I have tried both the following options

Do[Table[val[[All, i]] = Table[Max[enpva[[j]], (prf1[stsp] + rho*newp.val[[All, i + 1]])[[j]]], {j, 1, lensp}], {i, 1, tid}], {tid}];

Do[Table[val[[All, i]] = Max /@ Transpose[{enpva, (prf1[stsp] +    rho*newp.val[[All, i + 1]])}], {i, 1, tid}], {tid}];


Both take forever to run..and so I cannot proceed with the problem. I have purposely put in the ugly looking vectors of prf1[stsp], enpvc and enpva as these are my real calculations and I am not sure if these long numbers are causing the problem.

If you see lensp=2*tid+1 and with greater tid's (20 or 30) and with even uglier numbers, what is the recourse??

Any help would be greatly appreciated. Thank you.

This is rather an extended comment than an actual answer. I find your code very hard to read, in particular because you use very complicated syntax to achieve trivial goals. Here some examples:

Creating a matrix with a given constant value in all entries. Your code:

e1=0;
newp = Table[e1, {i, lensp}, {j, lensp}];


Alternative:

newp = ConstantArray[0, {lensp,lensp}];


Creating matrices with given entries on the kth diagonal. Your code:

e1=0;
newp = Table[e1, {i, lensp}, {j, lensp}];
Table[newp[[i, Min[i + 1, lensp]]] = pid, {i, lensp}];


Alternative:

newp=DiagonalMatrix[ConstantArray[pid, lensp], 1];
newp[lensp,lensp]=pid; (* are you sure you want this? *)


I couldn't quite follow as to what you want to achieve in the end. However, I suggest to apply the same concept of eliminating the Table and Do loops in the rest of the code.

• I can change newp your way with the command "Constant Array". Then "pid" and "qid" are probabilities and given the ith row of newp, I am changing the Min of (i+1th,last) column and the Max of (i-1th, first) column. So please keep that part unchanged. Now val=ConstantArray[0,{lensp,tid+1}], with the last column equal to the given vector enpvc. The goal of the problem is, starting backwards, you take the Max of (enpva, rho*newp.val(last column)). You are comparing element for element and taking the max to get the previous column of "val". U keep replacing until you get the first column of val. – Supratim Das Gupta Feb 4 '17 at 23:06
• I think it would really help if you illustrate your process step by step with the expected outcome on a toy data set of maybe 3 dimensions instead of 21 and integer values. – Felix Feb 4 '17 at 23:16
• Thanks..I had found my mistake. The probblem was with the command val[[All, tid + 1]] = tenpvc; It SHOULD HAVE BEEN enpvc (NOT the Table Form). Now the commands work. – Supratim Das Gupta Feb 11 '17 at 17:41