5 views (last 30 days)

Show older comments

Hi,

I have this code with divides a big cube into 27 equal sized cubes.

N=100; % Number of points

cubesize = 100; % meter

subdivision = 3; % == 27^(1/3)

subcubesize = cubesize/subdivision;

% Generare N points in big cube V :) (0,cubsize)^3

xyz=cubesize*rand(N,3);

% Compute the density of 27 small cubes

ijk = ceil(xyz/subcubesize);

n = accumarray(ijk,1,subdivision*ones(1,3));

density = n/subdivision^3 % #points per m^3 in each of 27 subcubes

close all

scatter3(xyz(:,1),xyz(:,2),xyz(:,3));

hold on

h = slice([0 cubesize],[0 cubesize],[0 cubesize],zeros(2,2,2),...

(0:3)*subcubesize,(0:3)*subcubesize,(0:3)*subcubesize);

set(h,'FaceColor','none')

axis equal

I am using Rand to distribute points in big cube. How can i count the number of points in each small cube and the distance of each point in small cube from all other points in that same small cube.

Keyur Mistry
on 22 Sep 2020

I understand you want to calculate number of points inside each sub cube and the distance between any two points inside a sub cube.

In the code provided by you, output variable ‘n’ is 3 x 3 x 3 matrix. In matrix ‘n’ there are 27 elements corresponding to each sub cube which gives number of points inside the sub cube. To find distance between any two points you can use the command ‘vecnorm’:

I hope this helps.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!