# How can I calculate the number of points in subcubes of a big cube with random distribution. How can I calculate the distance between points

4 views (last 30 days)
Tamoor Shafique on 19 Sep 2020
Commented: Tamoor Shafique on 26 Sep 2020
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.
Tamoor Shafique on 26 Sep 2020
close all % this is necessary if you do not use it every next simulation will keep updating the no. of nodes on previous plot hence no. of nodes will keep increasing from 100 to 200 to 300 in every simulation
clear all
xm=100;%maximum dimension on x-axis
ym=100;%maximum dimension on y-axis
zm=100;%maximum dimension on z-axis
x=0;% for clarity to see where is origin we will display this is zero for x
y=0;% for clarity to see where is origin we will display this is zero for y
z=0;% for clarity to see where is origin we will display this is zero for z
sink.x=0.5*xm; % if sink node is in middle of x axis of the network
sink.y=0.5*ym; % if sink node is in middle of y axis of the network
sink.z=0.5*zm; % if sink node is in middle of z axis of the network
n=100 % total number of nodes deployed
for i=1:1:n
S(i).xd=rand(1,1)*xm; %x-coordinated of the ith node
XR(i)=S(i).xd; %x-range of ith node
S(i).yd=rand(1,1)*ym; %y-coordinates of the ith node
YR(i)=S(i).yd; %y-range of ith node
S(i).zd=rand(1,1)*zm; %z-coordinates of the ith node
ZR(i)=S(i).zd; %z-range of ith node
S(n+1).xd=sink.x;% if the sink node is in the middle of network
S(n+1).yd=sink.y;% if the sink node is in the middle of network
S(n+1).zd=sink.z;% if the sink node is in the middle of network
figure(1)
plot3(x,y,z,xm,ym,zm,S(i).xd,S(i).yd,S(i).zd,'ob',S(n+1).xd,S(n+1).yd,S(n+1).zd,'*r'); % this will change if the sink location changes
title 'wireless sensor network'; %title of the figure
xlabel '(m)'; % label x-axis is in meters (m)
ylabel '(m)'; % label y-axis is in meters (m)
zlabel '(m)'; % label z-axis is in meters (m)
hold on; % always use after plot3 otherwise flattens the 3d view into 2D
grid on; % will display grid on the plot
end
%subdivision into small cubes
cubesize=xm, xm=ym, ym=zm, zm=xm;%overall network size the big cubic network
subdivision=3; %because diving the full cube into 27 small cubes will make it easier to determine the node distribution in small areas
subcubesize=xm/subdivision; %size of each small cube
h = slice([0 cubesize],[0 cubesize],[0 cubesize],zeros(2,2,2),...
(0:3)*subcubesize,(0:3)*subcubesize,(0:3)*subcubesize);%makes slices of the big cube into small cubes
set(h,'FaceColor','none')%makes the surface transparent so could view nodes distribution in the cubes
axis equal
Tamoor Shafique on 26 Sep 2020
How do I calculate number of nodes in each sliced subcube?