Skip to main content

plotting - A problem with set values



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:





  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.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?