Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'variables and for loops' topicStudent Support Forum > General > Archives > "variables and for loops"

Next Comment >Help | Reply To Topic
Author Comment/Response
GeorgeL
06/03/13 11:45am

I run the following bits of code into mathematica and it doesn't behave as I expect it to.

The following bit of code starts with a 5x5 table which I call x and creates another, y, which will depend on x. I also create a 16x1 table where each entry is 9000, which I will need later. This bit of code runs exactly as expected.

>>> x
>>> {{131, 201, 630, 537, 805}, {673, 96, 803, 699, 732}, {234, 342, 746, 497, 524}, {103, 965, 422, 121, 37}, {18, 150, 111, 956, 331}}

>>> y = Table[0, {i = 5}, {j = 5}]
>>> For[i = 1, i < 6, i++, y[[i, 1]] = Sum[x[[j, 1]], {j, 1, i}] ]
>>> For[i = 1, i < 6, i++, y[[1, i]] = Sum[x[[1, j]], {j, 1, i}] ]

>>> z = Table[9000, {i = 16}, {j = 1}]

The following I do not understand:

I run

For[w = 2, w < 6, w++,
For[i = 2, i < 6, i++,
For[j = 1, j < i + 1,
j++, {z[[((w - 2)*4) + i - 1]],
y[[i, w]]} = {Append[
z[[((w - 2)*4) + i - 1]], (y[[j, w - 1]] +
Sum[x[[k, w]], {k, j, i}])], Min[z[[((w - 2)*4) + i - 1]]]}]]

The code runs fine but and I get exactly what I want for z. However the Min function appears to take the minimum of the old list and not appended list. I find this strange as the following code, where initially p = {1} and q = 1,

>>> For[i = 1, i < 3, i++, {p, q} = {p = Append[p, -i], q = Min[p]}]

returns q=-2 which suggests it has taken the minimum after it has appended the lsat element.

Any help would be greatly appreciated.

Thanks

URL: ,

Subject (listing for 'variables and for loops')
Author Date Posted
variables and for loops GeorgeL 06/03/13 11:45am
Re: variables and for loops Bill Simpson 06/03/13 4:19pm
Re: Re: variables and for loops GeorgeL 06/04/13 5:29pm
Next Comment >Help | Reply To Topic