Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generate the random points as @PheuVerg suggested (with a slight vectorized tweak) </p> <pre><code>n = 8; %must be even! x = rand(n, 1)*2*R - R; y = rand(n, 1).*sqrt(R^2 - x.^2).*2-sqrt(R^2 - x.^2); </code></pre> <p>Then use kmeans clustering to get n/2 centers</p> <pre><code>[~ c] = kmeans([x y], n/2); </code></pre> <p>now you have to loop through each center and find it's distance to each point</p> <pre><code>dists = zeros(n, n/2); for cc = 1:n/2 for pp = 1:n dists(pp, cc) = sqrt((c(cc,1) - x(pp))^2 + (c(cc,2) - y(pp))^2); end end </code></pre> <p>now you must find the smallest 2 values for each columns of dists</p> <pre><code>[sorted, idx] = sort(dists); </code></pre> <p>so now the top two rows of each column are the two nearest points. But there could be clashes! i.e. points that are nearest to two different centers. So for repeated values you have to loop through and choose swap for the point that will give you the smallest extra distance.</p> <p>Example data:</p> <pre><code>x = 0.7894 -0.7176 -0.5814 0.0708 0.5198 -0.2299 0.2245 -0.8941 y = -0.0800 -0.3339 0.0012 0.9765 -0.4135 0.5733 -0.1867 0.2094 sorted = 0.1870 0 0 0.1555 0.2895 0.5030 0.5030 0.2931 0.3145 1.1733 0.6715 0.2989 1.0905 1.1733 0.7574 0.7929 1.1161 1.2326 0.8854 0.9666 1.2335 1.2778 1.0300 1.2955 1.2814 1.4608 1.2106 1.3051 1.4715 1.5293 1.2393 1.5209 idx = 5 4 6 3 7 6 4 2 1 3 3 8 6 7 8 6 3 8 7 7 2 1 2 4 4 5 1 5 8 2 5 1 </code></pre> <p>So now it's clear that <code>5</code> and <code>7</code> are pairs, and that <code>3</code> and <code>2</code> are pairs. But <code>4</code> and <code>6</code> are both repeated. (in this case it is clear that they are pairs too I guess!) but what I would suggest is to leave point <code>4</code> with center <code>2</code> and point <code>6</code> with center <code>3</code>. Then we start at column 2 and see the next available point is <code>8</code> with a distance of <code>1.2326</code>. This would leave point <code>1</code> paired with point <code>6</code> but then it's distance from the center is <code>1.2106</code>. Had we paired point <code>6</code> with <code>8</code> and point <code>4</code> with <code>1</code> we would have got distances of <code>0.7574</code> and <code>1.2778</code> respectively which is actually less total distance. So finding 'close' pairs is easy but finding the set of pairs with the globally smallest minimum is hard! This solutions gets you something decent quite easily but fi you need the global best then I'm afraid you have quite a bit of work to do still :(</p> <p>Finally let me add some visualisation. First lets (manually) create a vector that shows which points are paired:</p> <pre><code> I = [1 2 2 1 3 4 3 4]; </code></pre> <p>Remember that that will depend on your data! Now you can plot is nicely like this:</p> <pre><code>gscatter(x, y, I) </code></pre> <p>Hope this gets you close and that you can eliminate the manual pairing of mine at the end by yourself. It shouldn't be too hard to get a crude solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload