n维空间点分布调整:如何修改Matlab代码实现图A所示分布
Hey there! Let's break down how to tweak your existing code to generate the point distribution shown in Figure A. First, let's clarify what your original code produces: it generates uniformly random points inside an n-dimensional solid sphere (the rand(N,1).^(1/d) term ensures volume-based uniformity across the sphere's interior).
Assuming Figure A shows uniformly random points on the surface of an n-dimensional sphere (the most common contrast to your current code's output), here are the key changes you need to make:
Key Modifications
1. Fix the Radius to the Sphere's Surface
The original code uses radius = r * (rand(N,1).^(1/d)); to spread points throughout the sphere's volume. To lock all points onto the sphere's surface, replace this line with a fixed radius equal to r:
% Replace the original radius calculation with this radius = r * ones(N, 1); % All points sit exactly at radius r from the center
2. Simplify (Optional) the Vector Normalization
Your original code normalizes the random points to unit length before scaling. You can make this cleaner using MATLAB's built-in vecnorm function (available in R2017b and later):
% Replace the original X scaling line with this X = X .* (radius ./ vecnorm(X, 2, 2)) + C_rep; % For older MATLAB versions, keep your original logic but remove the rand-based radius
If Figure A Shows a Hypercube Distribution
If instead Figure A displays points uniformly distributed inside an n-dimensional hypercube (square in 2D, cube in 3D), you can completely replace the point-generation logic:
- Delete all lines from
X = randn(N,d);up toX = X.*repmat(...) + C_rep; - Add this line instead:
% Generate uniform points inside a hypercube centered at C_point, with side length 2r X = C_rep + (rand(N,d) - 0.5) * 2*r;
Verify the Change
After making these adjustments, run the code. The 2D plot will show all points lying exactly on the circle (for the sphere surface case) or filling a square (for the hypercube case), matching Figure A's distribution.
内容的提问来源于stack exchange,提问作者Shdotcom

