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 ?
Answer
There seem to be at least two issues here:
You are not resetting
ExtractionCon = {}inside the outerDoloop, thereforeDivided1grows longer thanPartition1With (1) corrected you will get a different error (repeated):
Set::setraw: Cannot assign to raw object 3. >>because thex*Symbols now have values, and they evaluate before the assignment is attempted.
There are likely a number of better ways to write your operation. For example Elegant manipulation of the variables list may be helpful to you. If you explain exactly what you are trying to do I shall try to show you how I would write it, and why.
Responding to the comment below I am guessing that you want something like this, but since I don't know what you are actually trying to accomplish I cannot be certain:
Do[
ExtractionCon = {};
Do[Extraction = Extract[{{1, 2}, {3, 4}, {5, 6}, {7, 8}}, {i + 1}];
Divided1 = AppendTo[ExtractionCon, Extraction];, {i, 1, 2}];
Clear["x*"];
SetFunc = MapThread[Set, {Partition1, Divided1}, 2];,
{j, 1, 3}
];
Again I request that you tell me your actual need. Also please see Alternatives to procedural loops and iterating over lists in Mathematica as you miss out on much of Mathematica's power if you write in explicit loops.
Comments
Post a Comment