6
$\begingroup$

There is a set of data in the form of a table that I import into Mathematica:

https://dropmefiles.com/7BXAE

a = Import["To.mat"];

a = Partition[Flatten[a], 3];

Then I plot $x,y$ on the plane:

data = Transpose@{a[[All, 1]], a[[All, 2]]};

And I get the following picture:

ListPlot[data]

enter image description here

Desired result that I need:

enter image description here

That is, I need the place where the points are concentrated (where they are built) to be represented as a contour or surface. How to do it with Mathematica commands?

$\endgroup$
6
  • 1
    $\begingroup$ I cannot import the file you provided. Is it MATLAB specific? $\endgroup$ Oct 17 at 16:53
  • 1
    $\begingroup$ When importing the data file Mathematica 13.1 (Windows 10) gives the error message "Data cannot be imported because it contains infinite or indeterminate values. Use the option "IndeterminateValues" to provide replacements." My attempts to provide "IndeterminateValues" were unsuccessful. $\endgroup$
    – JimB
    Oct 17 at 17:52
  • 1
    $\begingroup$ While you can probably get a desired result from using SmoothKernelDistribution, the apparent criterion for success is that it will look like what you did by hand as opposed to something more objective. How the data was collected and what are the consequences of not having an appropriate contour would be helpful to know. $\endgroup$
    – JimB
    Oct 17 at 17:56
  • 3
    $\begingroup$ For a workaround for the data: a = Import["To.mat", "IndeterminateValues" -> 999999] /. {999999. -> Nothing} /. {} -> Nothing; $\endgroup$
    – Bob Hanlon
    Oct 17 at 18:26
  • 1
    $\begingroup$ @JimB 1. The data were collected as follows: an inequality is given, and the space of its solutions is filled with uniformly distributed parameter values for which the inequality is satisfied. 2. The outline is necessary because the boundaries of this contour will be used in tasks of orientation of the robot in a complex environment. $\endgroup$
    – dtn
    Oct 17 at 19:52

5 Answers 5

11
$\begingroup$

The following will essentially duplicate what you did by hand (although the dataset you provided isn't quite the same dataset shown in your question).

If you need to have the contours more "shrink-wrapped" to the outside border or something completely automatic, then you'll need something else as I tried a few different values for the kernel bandwidth and the contour value.

a = Import["c:\\users\\drjba\\Downloads\\To.mat", 
     "IndeterminateValues" -> 999999] /. {999999. -> Nothing} /. {} -> Nothing;
skd = SmoothKernelDistribution[a[[1]], {0.25, 0.25}];
Show[ContourPlot[PDF[skd, {x, y}], {x, -6, 4}, {y, -5, 5}, 
  Contours -> {0.0023}, ContourShading -> None], ListPlot[a[[1]]]]

Data and contour plot

A completely automatic way is to allow the default bandwidth to be selected and then choose the contour that encompasses all points.

skd = SmoothKernelDistribution[a[[1]], "SheatherJones"];
pdf = PDF[skd, {#[[1]], #[[2]]}] & /@ a[[1]];
Show[ContourPlot[PDF[skd, {x, y}], {x, -6, 4}, {y, -5, 5},
  Contours -> {Min[pdf]}, ContourShading -> None,
  PlotPoints -> 100], ListPlot[a[[1]]]]

Contour that encompasses all data points with SheatherJones choice of bandwidth

$\endgroup$
1
  • $\begingroup$ thank you for this answer: very nice :) $\endgroup$
    – ubpdqn
    Oct 20 at 7:05
8
$\begingroup$

Using the free cloud account (since I don't have ConcaveHullMesh on v12.2.0-Win7-x64)

pts = RandomPoint[Annulus[{0, 0}, {50, 100}], 100];
{m1=ConcaveHullMesh[pts],m2= DelaunayMesh[pts]};
reg=BoundaryDiscretizeRegion[m1];
Show[reg,Graphics[{Red,MeshPrimitives[reg,1]}],Graphics[{Black,Point@pts}]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ +1 Very nice! I wonder if the robots need a little buffering around each point? Maybe: n = 20; radius = 1/4; bufferedPts = Flatten[Table[{#[[1]], #[[2]]} + Sqrt[RandomReal[]]* radius*{Cos[\[Theta]], Sin[\[Theta]]}, {\[Theta], 0, 2 \[Pi] (1 - 1/n), 2 \[Pi]/n}] & /@ pts, 1];. $\endgroup$
    – JimB
    Oct 18 at 1:34
8
$\begingroup$
  • Using the data a as @BobHanlon show in the comment.
  • Using RegionDilation to inflate every point.
a = Import["To.mat", 
     "IndeterminateValues" -> 999999] /. {999999. -> Nothing} /. {} ->
     Nothing;
reg = MeshRegion[a[[1]], Point /@ Range@Length@a[[1]]];
dilation = RegionDilation[reg, .3];
Show[Region[RegionBoundary[dilation], BaseStyle -> Red], 
 Region[reg, BaseStyle -> Blue]]

enter image description here

$\endgroup$
0
7
$\begingroup$

Using fake data since we can't access your actual data:

data = Flatten[Table[{x, y}, {x, -1, 1, .1}, {y, -1, 1, .1}], 1];
data // Short
(* {{-1., -1.}, {-1., -0.9}, <<437>>, {1., 0.9}, {1., 1.}} *)

Now, let's add some z data so we can generate contours:

Raise[{x_, y_}] := If[RegionDistance[Circle[], {x, y}] < .2, {x, y, 1}, {x, y, 0}];
newData = Map[Raise, data, {-2}];
newData // Short
(* {{-1., -1., 0}, {-1., -0.9, 0}, <<437>>, {1., 0.9, 0}, {1., 1., 0}} *)

Now ListContourPlot can do the job:

ListContourPlot[newData, Contours -> 1]

enter image description here

All of this assumes that you can augment your data as needed. It looks like your data is on a grid/lattice, so I assume that this is feasible.

$\endgroup$
1
  • $\begingroup$ ibb.co/rv2s23q I tried your code for my data (by the way, if you downloaded it to your computer, try using the file path in the import command via Insert - File Path) and I play with the outline settings. Unfortunately, the results are unsatisfactory (see link to the picture). Although for your "fake" matrix, the result is not bad. $\endgroup$
    – dtn
    Oct 17 at 19:49
3
$\begingroup$

I just learned about ReconstructionMesh. So, taking a similar approach as Syed:

pts = RandomPoint[Annulus[{0, 0}, {50, 100}], 100];
reg = BoundaryDiscretizeRegion[ReconstructionMesh[pts]];
Show[reg, Graphics[{Red, MeshPrimitives[reg, 1]}], Graphics[{Black, Point@pts}]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you for your answer! I think it's time to think about switching to a new version of Mathematica for me. Because most of the commands proposed here do not work in the old 12 PC-version. They only work in the cloud version But every way is good! $\endgroup$
    – dtn
    Oct 20 at 15:45

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.