MATLAB Answers


how i can generate random points inside a cube?

Asked by ziba
on 25 Sep 2012
Latest activity Answered by humaira akber
on 15 Dec 2013

i want to generate rondom points inside a cube.



No products are associated with this question.

2 Answers

Answer by Wayne King
on 25 Sep 2012
Edited by Wayne King
on 25 Sep 2012
 Accepted answer

You can use rand()

 Z = rand(10,10,10);

The above gives you the unit cube. You can translate this and scale the side length:

 Z = 2+(4-2)*rand(10,10,10);

  1 Comment

Hi Wayne, I would understand

Z= rand(n,n,3); 

which could be plotted using


Each point would then have 3 random coordinates. How would Z = rand(10,10,10) work and how would the coordinates be assigned?

Answer by humaira akber
on 15 Dec 2013

npx=12; % no. of NPs on x-axis npy=12; % no. of NPs on y-axis npz=12;

dx=1; % distance b/w two NPs on x-axis dy=1; % distance b/w two NPs on y-axis dz=1;

x(1)=dx; % first particle is at dx from y-axis y(1)=dy; % first particle is at dx from x-axis z(1)=dz; np=0; x=zeros(1,npx); for k=1:npx x(k)=k*dx; y=zeros(1,npy); for l=1:npy y(l)=l*dy; z=zeros(1,npz); for m=1:npz z(m)=m*dz; end

sir how to plot cube by using this assembly please give me answer as earlier as you can please.will be waiting


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!