3
$\begingroup$

I have two list of data

vdata = {0., 0.2, 0.4, 0.6, 0.8, 1.};

sol = {{-96.29,-0.07,-0.00,0.95}, {-32.29,-0.06,-0.00},{-32.29,-0.03,-0.03,0.95},{-32.29,0.95},{-32.29,42.80}, {-32.29, 0.95}};

The sol is the corresponding solutions for vdata , so for a given element in vdata there can be 2 or 3 or 4 (Eg. size of the nested list is different) solutions.

I want to select only the solutions that lie in the range 0<=sol<=1, and plot it with the corresponding vdata (also note that for vdata=0.8 sol={-32.29,42.80} so this does not satisfy the constraints so should not be included in the plot).

Please help

Thanks

$\endgroup$
  • 1
    $\begingroup$ Try Select[Between[{0, 1}]] /@ sol. $\endgroup$ – J. M. will be back soon Sep 25 '18 at 10:30
  • $\begingroup$ @J.M.issomewhatokay. Its working :-). How to select the corresponding vdata simultaneously ? As for vdata=0.8 its giving empty list. $\endgroup$ – T S Singh Sep 25 '18 at 12:57
  • $\begingroup$ @J.M.issomewhatokay. For removing the empty list I found a command Select[list, UnsameQ[#, {}] &] , but how about to find the corresponding vdata $\endgroup$ – T S Singh Sep 25 '18 at 13:01
1
$\begingroup$

You can try with:

MapThread[{#1, #2} &, {vdata, sol //. {x_ /; ((x > 1) || (x < 0)) -> Nothing}}]

However, note that the conditions you give do not always result in a single choice.

{{0., {0., 0.95}}, {0.2, {0.}}, {0.4, {0.95}}, {0.6, {0.95}}, {0.8, {}}, {1., {0.95}}}

Which do you actually want to select?

$\endgroup$
1
$\begingroup$
data = Catenate[MapThread[Thread@*List, {vdata, Pick[sol, sol - Clip[sol, {0, 1}], 0.]}]]

{{0., 0.}, {0., 0.95}, {0.2, 0.}, {0.4, 0.95}, {0.6, 0.95}, {1., 0.95}}

ListPlot[data]

$\endgroup$
1
$\begingroup$
vs = (Thread /@ Transpose[{vdata, Select[0 <= # <= 1 & ] /@ sol}]) /. {} -> {Missing[]};
ListPlot[vs, PlotMarkers -> {Automatic, 12}, 
 GridLines -> {None, vdata}, PlotLegends -> (ToString /@ vdata)]

enter image description here

$\endgroup$
1
$\begingroup$

Another method

Thread /@ DeleteCases[DeleteCases[MapThread[List, {vdata, sol}], x_?NumericQ /; 0 <= x <= 1 // Except, {3}], {_, {}}]
ListPlot[%, PlotStyle -> PointSize[.02]]

{{{0., 0.}, {0., 0.95}}, {{0.2, 0.}}, {{0.4, 0.95}}, {{0.6, 0.95}}, {{1., 0.95}}}

enter image description here

$\endgroup$

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.