The following code is just an example to clearly explain my problem.
Assume that :
In[1]:= Vars = Table[Symbol["x" <> ToString[i]], {i, 1, 4}];
In[2]:= Partition1 = Partition[Vars, 2];
Which the Results are :
Out[1]:= {x1, x2, x3, x4}
Out[2]:= {{x1, x2}, {x3, x4}}
Now, I want to do a calculation using above and following codes like below :
ExtractionCon = {};
Do[
Do[
Extraction = Extract[{{1, 2}, {3, 4}, {5, 6}, {7, 8}}, {i + 1}];
Divided1 = AppendTo[ExtractionCon, Extraction];
, {i, 1, 2}];
SetFunc = MapThread[Set, {Partition1, Divided1}, 2];
, {j, 1, 3}];
SetFunc
When I run this code, following error appears :
MapThread::mptc: Incompatible dimensions of objects at positions {2, 1} and {2, 2} of
MapThread[Set,{{{3,4},{5,6}},{{3,4},{5,6},{3,4},{5,6}}},2]; dimensions are {2,2} and
{4,2}. >>
MapThread::mptc: Incompatible dimensions of objects at positions {2, 1} and {2, 2} of
MapThread[Set,{{{3,4},{5,6}},{{3,4},{5,6},{3,4},{5,6},{3,4},{5,6}}},2]; dimensions
are {2,2} and {6,2}. >>
Out[5]:= MapThread[Set, {{{3, 4}, {5, 6}}, {{3, 4}, {5, 6}, {3, 4}, {5, 6}, {3,
4}, {5, 6}}}, 2]
Clearly, the problem is MapThread ( SetFunc = MapThread[Set, {Partition1, Divided1}, 2];). It sets the values {x1,x2}={3,4} and {x3,x4}={5,6}. For the first Iteration (j=1),it works perfectly, but for more than one Iteration ( {j,1,3}), it doesn't. I have already used Clear[] or ClearAll[], baut I couldn't fixed it.
How is it possible to unset and reset the values to do the iterations correctly ?
-EDITED-
As Mr.Wizard requested, I put here a better code (I hope!) which unnecessary parts are omitted. I have already add Mr.Wizard 's suggestions and corrections into the following code. The inputs are :
Npop = 2;
NPart = 3;
L = 1;
FEN = 16;
Which are Npop=Number of elements of the Chrom, NPart=Number of intervals that I want to have in the outputs, L= The unit length that I want to extraxt my intervals from it and FEN= The number of whole intervals .
Chrom = {{0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0}, {1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1}};
varsP = Table[Symbol["x" <> ToString[i]], {i, 1, 2 NPart}];
Partitionvars = Partition[varsP, 2];
DivideLenghth = Range[0, L, N[L/FEN]];
ElementPosition = Partition[DivideLenghth, 2, 1];
NumberOfPositionCon = {};
Chrom is my input with multi-elements (not just one element) for the bellow function:
Clear[DoFunction];
DoFunction[Chrom_] :=
Block[{sigma1, Extraction, SplitChrom, NumberOfPosition,
PositionsOn, ExtractionDL, DivLen, SetEqual, List1},
sigma1 = {};
Do[
ExtractionDLCon = {};
Extraction = Extract[Chrom, {r}];
SplitChrom = Partition[Extraction, Log2[FEN]];
Do[
NumberOfPosition = FromDigits[SplitChrom[[i]], 2];
PositionsOn = AppendTo[NumberOfPositionCon, NumberOfPosition];
ExtractionDL = Extract[ElementPosition, (PositionsOn[[i]] + 1)];
DivLen = AppendTo[ExtractionDLCon, ExtractionDL];
Clear["x*"];
, {i, 1, NPart}];
SetEqual = MapThread[Set, {Partitionvars, DivLen}, 2];
List1 = AppendTo[sigma1, SetEqual];
, {r, 1, Npop}];
List1];
When use DoFunction[] with input Chrom, the result will be :
In[1]:=DoFunction[Chrom]
Out[1]:= {{{0.0625, 0.125}, {0.125, 0.1875}, {0.5, 0.5625}}, {{0.0625,
0.125}, {0.125, 0.1875}, {0.5, 0.5625}}}
But the correct answer for In[1] must be :
The Correct Answer=: {{{0.0625`, 0.125`}, {0.125`, 0.1875`}, {0.5`, 0.5625`}},
{{0.5625`, 0.625`}, {0.625`, 0.6875`}, {0.6875`, 0.75`}}}
It seems that SetEqual keeps the values and doesn't reset and save new values for next iterations. Could you please help me with this problem ?
ExtractionCon = {}inside the outer Do loop ? Without correcting(1), I usedClear[]in my loop to clearx*values, but it didn't work. What is wrong with it? – Shellp Apr 16 '14 at 21:52{1,0,0,1}, it will work perfectly.But when I want to do some iterations and inputs become more than one like{{1,1,1,},{1,0,0,1}}, it will face some errors or trap inside a loop! One of that error was a code similar to above code! – Shellp Apr 17 '14 at 07:34