# Select element in unequal nested list and plot

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

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

## 4 Answers

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?

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]


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


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}}}