0

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 ?

Shellp
  • 523
  • 3
  • 13

1 Answers1

2

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:

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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 07: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 07: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 07: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 07:56
  • Yes, you're right! In fact, I am a beginner and I would be so happy if you could show me a better style in which to implement my codes and algorithm. Thank you so much again. I will edit this post and add a better example (By considering your corrections in the code).However, I will omit time-consuming and unnecessary parts in the code in order to focus on the problematic part. – Shellp Apr 17 '14 at 08:21
  • I edited this post and add a better code (I hope!). Thank you so much! :) – Shellp Apr 17 '14 at 09:09
  • @Shellp I'll try to get around to this in a few hours. – Mr.Wizard Apr 17 '14 at 23:51
  • @Shellp Sorry, I still don't have time to answer this properly. I've been upgrading the OS on an old machine and its fighting me every step of the way. – Mr.Wizard Apr 19 '14 at 07:13
  • @ Mr.Wizard : Take your time, please !! I'll be waiting for your answer. I have already find an answer and solved my problem. However, I'm sure that you will give an elegant solution for my question. I'm looking forward to hearing from you! :) – Shellp Apr 19 '14 at 13:30