The function initDataTable
simply reset all data to Black
first, then set the bottom right quarter to Blue
.
dataTable = Table[1, 10, 10];
someCostlyFunction[] := Module[
{a},
For[i = 1, i < 1000, i++, a += i];
a
];
changeColor[coord_] := Module[
{},
someCostlyFunction[]; (*If comment this line, the result is correct*)
RGBColor[0, 0, 1]
];
initDataTable[] := Module[
{},
(*first reset all data to zero*)
For[i = 1, i <= 10, i++,
For[j = 1, j <= 10, j++,
dataTable[[i]][[j]] = RGBColor[0, 0, 0];
]
];
(*change some data based on some logics*)
For[i = 6, i <= 10, i++,
For[j = 6, j <= 10, j++,
dataTable[[i]][[j]] = changeColor[{i, j}];
];
];
];
initDataTable[]
ArrayPlot@dataTable
The output are all black, the function call changeColor
looks failed. If I comment out the code someCostlyFunction
within changeColor
, the data table initialization is correct.
My Mathematica project become bigger and bigger, so I need to use nested Module calls, hope someone could help me solve this problem.
someCostlyFunction[]:=Module[{a},For[i=1,i<1000,i++,a+=i];a];
where you are incrementing the uninitialized valuea
a thousand times. If you change that tosomeCostlyFunction[]:=Module[{a=0},For[i=1,i<1000,i++,a+=i];a];
and do not comment out your costly function then some of your problems appear to go away. I believe there is still at least one more problem in your code, but this might be a good first step. $\endgroup$