Message Boards Message Boards

0
|
3360 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Array not being populated with values properly

Posted 11 years ago

Hi,

I have a code where the arrays (array lamda with 5 values) are being populated with values through a loop.

But after I run the program and when I ask the values of the array created it shows me an output like this:

{HoldPattern[lamda[1]] :> 0.5, HoldPattern[lamda[2]] :> 0.25, 
 HoldPattern[lamda[3]] :> 0.166666, HoldPattern[lamda[4]] :> 0.125, 
 HoldPattern[lamda[5]] :> 0.1}

Anyone nows why this should happen? Why is giving me this HoldPattern function before the value?

thanks

POSTED BY: Rafael Dettogni
4 Replies

Please show the code that caused this problem. Just showing the output makes it hard to diagnose what might be wrong in your original code.

POSTED BY: David Reiss
Posted 11 years ago

The code is below.

happens with every array:

tmax = 20;
tmin = 1;
patchnum = 5;
xmax = 10;
xcrit = 3;

alpha[1] = 1;
alpha[2] = 1;
alpha[3] = 1;
alpha[4] = 1;
alpha[5] = 1;

betai[1] = 0.05;
betai[2] = 0.04;
betai[3] = 0.03;
betai[4] = 0.02;
betai[5] = 0.01;


lamda[1] = 0.5;
lamda[2] = 0.25;
lamda[3] = 0.166666;
lamda[4] = 0.125;
lamda[5] = 0.1;


Y[1] = 1;
Y[2] = 2;
Y[3] = 3;
Y[4] = 4;
Y[5] = 5;

Array[beh, {19, 10}];
Array[f, {20, 10}];

For[t = tmax, t >= 1, t--,
 For[x = xcrit, x <= xmax, x++,
  If [t == tmax,
   If [x <= xcrit,
     f[t, x] = 0, f[t, x] = 1
    ]];
  If [t < tmax,
   maxfit = -5;
   For[patch = 1, patch <= patchnum, patch++,
    x1 = x - alpha[patch] + Y[patch];
    x2 = x - alpha[patch];
    If [x1 > xmax, x1 = xmax];
    If [x1 < xcrit,
     x1 = xcrit
     ];
    If [x2 > xmax, x2 = xmax];
    If [x2 < xcrit, x2 = xcrit];
    tempfit = (1 - 
        betai[patch])*(lamda[patch]*
         f[(t + 1), x1] + (1 - lamda[patch])*f[(t + 1), x2]);
    If [x2 <= xcrit,
     tempfit = (1 - betai[patch])*(lamda[patch]*f[(t + 1), x1]);] ; 
    If [tempfit > maxfit,
     maxfit = tempfit;
     beh[t, x] = patch;
     ];
    f[t, x] = maxfit;
    ]
   ]
  ]
 ]
POSTED BY: Rafael Dettogni

Array items or specified using double brackets [[]]. Function arguments are specified using single brackets [].

POSTED BY: David Reiss
Posted 11 years ago

Thanks again! Solved the problem. I was defining functions, not arrays.

solved!

POSTED BY: Rafael Dettogni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract