1
$\begingroup$

In the code below, I have values printed in output coordinate (coor) and solution vector, ϕvalue.

I want to plot for coordinates and corresponding solution values below.

 coor = {0.,6000., 2000., 4000., 8000., 10000., 6000., 12000., 8000., 10000.}
 ϕvalue = {{1.}, {0.178533}, {1.0287}, {0.709987}, {}, {},
    {0.138083}, {0.00703084}, {-0.059974}, {0.01218}}
 

For that I have removed the empty solution ({}) values from the ϕvalue vector by using DeleteCases and Flatten. But unfortunately I could not delete or remove the respective coordinate values. Because of that I am not getting the corresponding ϕvalue for the coordinate which are repeating and also solution ϕvalue values for repeating coordinates are getting interchanged in between.

See below output which is not in correct pair.

{{0.,1.}, {0.,1.}, {2000.,1.0287}, {4000.,0.709987}, {6000.,-0.059974},
  {6000.,0.178533}, {8000.,0.138083}, {10000.,0.00703084}, {12000.,0.01218}}

My question is how to skip the every 5th and 6th coordinate value and also the cooresponding empty solution value?

I have already checked the Drop and Exclusion commands, but could not make them work.

ClearAll[postProcess]
postProcess = # /. 
  Line -> Function[{x}, Line /@ Split[x, First[#] != First[#2] &]] &;
  << AceGen`; << AceFEM`; << MaTeX`;
name = StringDrop[NotebookFileName[] // FileNameSplit // Last, -3];

SMTInputData[];
plots = {};
nel = 2;
h = 1/nel;
L = 12000;
SMTAddDomain[{"\[CapitalOmega]", 
  name, {"\[Omega] *" -> 0.5, "ff*" -> 0}}];
SMTMesh["\[CapitalOmega]", "L1", {1/h}, {{0, 0}, {L, 0}}];
SMTAddEssentialBoundary["ID" == "AM" && "X" == 0 &, 1 -> 1];
SMTAddEssentialBoundary["ID" == "AP" && "X" == 0 &, 1 -> 1];
SMTAnalysis[];
            
coor = SMTNodeData["X"][[nel+4;; ,1]]
(Debug) Out[18]= {0., 6000., 2000., 4000., 0., 8000., 10000., 6000., 12000., 8000., 10000.}
coor // Dimensions;
ϕvalue = SMTNodeData["at"][[nel+4;;]]
(Debug) Out[20]= {{1.}, {0.178533}, {1.0287}, {0.709987}, {1.}, {}, {},{0.138083}, {0.00703084}, {-0.059974}, {0.01218}}
ϕvalue // Dimensions;
p = DeleteCases[{_, {}}] @\ ϕvalue
          
(Debug) Out[22]= {{1.}, {0.178533}, {1.0287}, {0.709987}, {1.}, {}, {},{0.138083}, {0.00703084}, {-0.059974}, {0.01218}}
y = Flatten[p]
(Debug) Out[23]= {1., 0.178533, 1.0287, 0.709987, 1., 0.138083, 0.00703084, -0.059974, 0.01218}
plot = Table[{coor[[i]], y[[i]]}, {i, 1, Length[y]}]
(Debug) Out[24]= {{0.,1.}, {6000.,0.178533}, {2000.,1.0287}, {4000.,0.709987}, {0.,1.}, {8000.,0.138083}, {10000.,0.00703084}, {6000.,-0.059974}, {12000.,0.01218}}
 plotSort = SortBy[plot, First]
(Debug) Out[25]= {{0.,1.},{0.,1.},{2000.,1.0287},{4000.,0.709987},{6000.,-0.059974},{6000.,0.178533},{8000.,0.138083},{10000.,0.00703084},{12000.,0.01218}}
$\endgroup$
2
$\begingroup$
cϕ = SortBy[First] @ Select[Length@# == 2 &]@Join[List /@ coor, ϕvalue, 2];


ListLinePlot[cϕ]

enter image description here

Alternatively,

cϕ2 = SortBy[First][Flatten /@ Select[FreeQ[{}]]@Transpose[{coor, ϕvalue}]];

cϕ3 = SortBy[First][Flatten /@ DeleteCases[{_, {}}]@Transpose[{coor, ϕvalue}]];

cϕ == cϕ2 == cϕ3
 True
| improve this answer | |
$\endgroup$
  • $\begingroup$ SortBy[First] @ Select[Length@# == 2 &]@Join[List /@ coor, ϕvalue, 2] could you please explain the syntax from Select[]? $\endgroup$ – user75507 Nov 15 at 17:32
  • 1
    $\begingroup$ @user75507, SortBy[First] @ Select[Length@# == 2 &]@Join[List /@ coor, ϕvalue, 2] is an alternative way to write SortBy[ Select[Join[List /@ coor, ϕvalue, 2],Length@# == 2 &], First]. The part Join[List /@ coor, ϕvalue, 2] gives a list of lists; the elements that correspond to {} in ϕvalue have length 1 and the ones we want have length 2; hence we use the selector function Length[#] ==2&. $\endgroup$ – kglr Nov 15 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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