Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 = {};


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}];


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 ?


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:

DoFunction[Chrom_] :=
Block[{sigma1, Extraction, SplitChrom, NumberOfPosition, 
PositionsOn, ExtractionDL, DivLen, SetEqual, List1},

sigma1 = {};


ExtractionDLCon = {};

Extraction = Extract[Chrom, {r}];

SplitChrom = Partition[Extraction, Log2[FEN]];


 NumberOfPosition = FromDigits[SplitChrom[[i]], 2];
 PositionsOn = AppendTo[NumberOfPositionCon, NumberOfPosition];

 ExtractionDL = Extract[ElementPosition, (PositionsOn[[i]] + 1)];

 DivLen = AppendTo[ExtractionDLCon, ExtractionDL];


 , {i, 1, NPart}];

SetEqual = MapThread[Set, {Partitionvars, DivLen}, 2];

List1 = AppendTo[sigma1, SetEqual];

, {r, 1, Npop}];


When use DoFunction[] with input Chrom, the result will be :


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 ?

share|improve this question
up vote 2 down vote accepted

There seem to be at least two issues here:

  1. You are not resetting ExtractionCon = {} inside the outer Do loop, therefore Divided1 grows longer than Partition1

  2. With (1) corrected you will get a different error (repeated): Set::setraw: Cannot assign to raw object 3. >> because the x* 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:

  ExtractionCon = {};
  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}

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.

share|improve this answer
Could you please elaborate it more ? How it is possible to resetting ExtractionCon = {} inside the outer Do loop ? Without correcting(1), I used Clear[] in my loop to clear x* values, but it didn't work. What is wrong with it? – Shellp Apr 16 '14 at 21:52
Thank you Mr.Wizard !! It works perfectly here. I can not believe that how much it was simple and I couldn't do that! – Shellp Apr 17 '14 at 7:11
In fact, this code is just a sample. The original code is something else that I'm trying to write a Fitness Function for a Genetic Algorithm with Mathematica Software. As you know, when the lines of codes grow, the probability of errors will increase. However, I still have problems in the whole code even with the correction of above code. I am trying to find my mistakes or errors. – Shellp Apr 17 '14 at 7:23
I have written a code that when the input is just a single array like {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 7:34
@Shellp Please don't take offense at what I am about to say, because Mathematica is really quite unlike most other languages. You are at a rudimentary stage of Mathematica programming, and I am seeking a way to at least illustrate a better style in which to implement your algorithm. If you are able to give a larger example of what you wish a specific function to accomplish (and the sooner you start thinking in terms of functions the better, re: Mathematica), I shall try. For now this recent post by WReach would be a good start. – Mr.Wizard Apr 17 '14 at 7:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.