私はあなたの最後の点だけに集中します
(3)ヒルベルト空間充填曲線を使用[0,1]x[0,1]
して、ドロー イン内の各ドロー インを変換します[0,1]
。ヒルベルト曲線マッピングでは、ドロー イン[0,1]x[0,1]
は 内の 1 つ (または全射性のために複数) の点のイメージになり[0,1]
ます。この中から1点選びたいと思います。これを行うMatlabにビルド済みのパッケージはありますか?
私の知る限り、Matlab にはこれを行うビルド済みのパッケージはありませんが、ウィキペディアのコードは MATLAB から呼び出すことができ、変換ルーチンとゲートウェイ関数を組み合わせるのと同じくらい簡単です。xy2d.c
ファイル内:
#include "mex.h"
// source: https://en.wikipedia.org/wiki/Hilbert_curve
// rotate/flip a quadrant appropriately
void rot(int n, int *x, int *y, int rx, int ry) {
if (ry == 0) {
if (rx == 1) {
*x = n-1 - *x;
*y = n-1 - *y;
}
//Swap x and y
int t = *x;
*x = *y;
*y = t;
}
}
// convert (x,y) to d
int xy2d (int n, int x, int y) {
int rx, ry, s, d=0;
for (s=n/2; s>0; s/=2) {
rx = (x & s) > 0;
ry = (y & s) > 0;
d += s * s * ((3 * rx) ^ ry);
rot(s, &x, &y, rx, ry);
}
return d;
}
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int n; /* input scalar */
int x; /* input scalar */
int y; /* input scalar */
int *d; /* output scalar */
/* check for proper number of arguments */
if(nrhs!=3) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Three inputs required.");
}
if(nlhs!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required.");
}
/* get the value of the scalar inputs */
n = mxGetScalar(prhs[0]);
x = mxGetScalar(prhs[1]);
y = mxGetScalar(prhs[2]);
/* create the output */
plhs[0] = mxCreateDoubleScalar(xy2d(n,x,y));
/* get a pointer to the output scalar */
d = mxGetPr(plhs[0]);
}
でコンパイルしmex('xy2d.c')
ます。
上記の実装
[...] nセルでnセルに分割された正方形を想定しています。nは 2 の累乗で、左下隅に (0,0)、上隅に ( n -1, n -1) の整数座標があります。右隅。
実際には、マッピングを適用する前に離散化ステップが必要です。すべての離散化問題と同様に、精度を賢く選択することが重要です。以下のスニペットは、すべてをまとめたものです。
close all; clear; clc;
% number of random samples
NSAMPL = 100;
% unit square divided into n-by-n cells
% has to be a power of 2
n = 2^2;
% quantum
d = 1/n;
N = 0:d:1;
% generate random samples
x = rand(1,NSAMPL);
y = rand(1,NSAMPL);
% discretization
bX = floor(x/d);
bY = floor(y/d);
% 2d to 1d mapping
dd = zeros(1,NSAMPL);
for iid = 1:length(dd)
dd(iid) = xy2d(n, bX(iid), bY(iid));
end
figure;
hold on;
axis equal;
plot(x, y, '.');
plot(repmat([0;1], 1, length(N)), repmat(N, 2, 1), '-r');
plot(repmat(N, 2, 1), repmat([0;1], 1, length(N)), '-r');
figure;
plot(1:NSAMPL, dd);
xlabel('# of sample')