TransWikia.com

sorting out the coordinate and respective solution values for plotting

Mathematica Asked on July 31, 2021

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.

(Debug) Out[18]= {0., 6000., 2000., 4000., 0., 8000., 10000., 6000., 12000., 8000., 10000.}
(Debug) Out[20]= {{1.}, {0.178533}, {1.0287}, {0.709987}, {1.}, {}, {},{0.138083}, {0.00703084}, {-0.059974}, {0.01218}}
(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}

One Answer

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

Correct answer by kglr on July 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP