![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282543/image.png)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
I am trying to create a normal random moving point for each individual point in a 3D scatter point. How do I go about to do this?
1 view (last 30 days)
Show older comments
I am trying to replicate the random movements (gaussian distribution) found in particles and to do that, I have already made a moving plot (in a normal random distribution) but I am trying to have a maybe 5 by 5 number of points with each point having its on random moving point.
Code attached are the moving plot and the scatter plot
2 Comments
darova
on 7 Apr 2020
Can you be more specific? Here is the result of one of your script
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282543/image.png)
How do you want it to look like?
sui zhi lau
on 7 Apr 2020
Hi Darova,
Thanks for replying! For the moving plot, this would be counted as the movement for one 'particle'. So would it be possible for multiple points to be moving cocurrently in the same plot.
So for the no magnetic field code, I would like to have each point's arrow moving randomly. so it would be:![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282551/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282551/image.png)
But with the same moving plot for each points on this plot.
Accepted Answer
darova
on 7 Apr 2020
What about this?
n = 5;
[X,Y] = meshgrid(1:n); % create mesh
T = 180*rand(n)-90; % initial angles
cla
plot(X(:),Y(:),'ob') % plot starting points
hold on
for i = 1:30
T = T + rand(n)*90 - 45; % increase/decrease angle
X = X + 0.05*cosd(T); % increase/decrease X coordinate
Y = Y + 0.05*sind(T); % increase/decrease Y coordinate
plot(X(:),Y(:),'.r') % plot new coordinates
pause(0.1)
end
hold off
24 Comments
sui zhi lau
on 7 Apr 2020
Hi Darova,
Yes! This was what I kind of wanted but I am doing a project on particle 'jumps' so would it be possible for the moving dots to be arrows? And shouldn't the plots be jumping towards the centre after awhile due to it being a gaussian distribution?
Thanks so much for helping again.
Ash
darova
on 7 Apr 2020
- would it be possible for the moving dots to be arrows?
Sure, do you know how quiver functoin works?
- And shouldn't the plots be jumping towards the centre after awhile due to it being a gaussian distribution?
Im bad at math. Can you show it on the picture? How should they move?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282594/image.png)
sui zhi lau
on 7 Apr 2020
Yes I have been using the quiver function for my initial 2D moving plot.
So, the arrows should be moving in such a way that points should intercept alot back into the middle.
That is why the arrows in my moving plot 'jumps' back close to the centre.
So I am expecting my code to have my scatter plot to look something like this with individual random movement (if it is a 5 x 5 matrix):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282600/image.png)
darova
on 7 Apr 2020
Was not that easy as i thought!
nmesh = 4;
niter = 40;
[X,Y] = meshgrid(0:nmesh-1); % create mesh
X = 5*X;
Y = 5*Y;
dx = normrnd(0,1,[nmesh^2 niter]);
dy = normrnd(0,1,[nmesh^2 niter]);
cla
plot(X(:),Y(:),'ob') % plot starting points
hold on
plot(X,Y,'k') % plot grid
plot(X',Y','k')
for i = 1:niter-1
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
quiver(X1, Y1, dx(:,i+1)-dx(:,i), dy(:,i+1)-dy(:,i))
% plot([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
% [Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]','.-r')
pause(0.5)
end
hold off
sui zhi lau
on 8 Apr 2020
Thank you so much Darova! That is the code that I want!
Just a side note:
Would it be possible if I change the distance between the 'particlles'. Because when I was changing:
X = 5*X;
Y = 5*Y;
into
X = 10*X;
Y = 10*Y;
It went out of sync with the particles' jumps
darova
on 8 Apr 2020
It's because of quiver scale. If you uncomment this part
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282801/image.png)
You will see the actual movement of your particles
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282802/image.png)
Try to scale quiver
scale = 0.5;
quiver(X1, Y1, dx(:,i+1)-dx(:,i), dy(:,i+1)-dy(:,i),scale)
sui zhi lau
on 8 Apr 2020
Oh sorry, I set the limit for the x and y coordinate. That was my mistake.
okay it works and I am happy with the answer you gave me!
But if possible, if I wanted to put it into a 3D format, what would I need to change/add?
sui zhi lau
on 8 Apr 2020
Nope ! more like the previous codes but rather than the arrows moving in 2 directions, they are moving in a 3 dimensional area in like a for example 5 by 5 by 5 particles.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282808/image.png)
So still something like this but the arrows are moving in 3 directions (x,y and z coordinates)
darova
on 8 Apr 2020
What if just add
dz = normrnd(0,1,[nmesh^2 niter]);
%
Z1 = 0 + dz(:,i);
And use quiver3?
sui zhi lau
on 8 Apr 2020
Yes I tried that but it kept showing errors
nmesh = 5;
niter = 40;
scale = 0.5;
[X,Y,Z] = meshgrid(0:nmesh-1); % create mesh
X = 5*X;
Y = 5*Y;
Z = 5*Z;
dx = normrnd(0,1,[nmesh^2 niter]);
dy = normrnd(0,1,[nmesh^2 niter]);
dz = normrnd(0,1,[nmesh^2 niter]);
cla
plot3(X(:),Y(:),Z(:),'ob') % plot starting points
hold on
%plot(X,Y,'k') % plot grid
%plot(X',Y','k')
for i = 1:niter-1
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
Z1 = 0 + dz(:,i);
quiver3(X1, Y1, Z1, dx(:,i+1)-dx(:,i), dy(:,i+1)-dy(:,i),dz(:,i+1)-dz(:,i))
%plot([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
%[Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]','.-r')
pause(0.5)
end
hold off
rotate3d on
sui zhi lau
on 24 Apr 2020
Edited: sui zhi lau
on 24 Apr 2020
hi darova,
This is a long shot but would it be posisble to add certain range of values to converge to the centre of the plot but it does not show any changes.
I have attached my code and a rough guide as to how I would like my point to converge to the centre with arrows.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287078/image.png)
I would like the points at the red box to change from random moving points to points moving towards the centre (black box)
I have attached my MATLAB file and hope that my explanation of my problem makes sense.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
%%
scale = 0.5;
k = -1;
y = 1;
nmesh = 5;
niter = 40; %no. of jumps
s = 35.3;% size of particle -> diameter = 45 nm , area of circle = 35.343 nm^2
[X,Y] = meshgrid((-nmesh+1):nmesh-1); % create mesh of -X -> X, -Y -> Y
X = 10*X;
Y = 10*Y;
dx = normrnd(0,1,[(size(X,1))^2 niter]); %
dy = normrnd(0,1,[(size(X,1))^2 niter]);
MagF=X/2;
countmax = size(X,1);
cla
%plot(X(:),Y(:),'ob')% plot starting points
%%
grid off
hold on
%plot(X,Y,'k') % plot grid
%plot(X',Y','k')
%%
for i = 1:niter-1
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
if (-MagF < X < MagF)
%U = X - X;
%V = Y - Y;
U = k.* ones(1,length(X))
V = zeros(1,countmax)
u = U;
v = V;
u(1:(countmax-1))=0
v(1:(countmax-1))=0
q1 = quiver(X1,Y1,u,v)
q1.LineWidth = s;
hold on
grid on
%%
else
% q = quiver(X1, Y1, (dx(:,i+1)-dx(:,i))*1.1, 1.1*(dy(:,i+1)-dy(:,i)),scale)
q.ShowArrowHead = 'off';
q.LineWidth = 3;
% plot([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
% [Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]','.-r')
pause(0.5)
%% percentage readings
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('White Space = %0.3f%%',percentageofwhite(y)),'Units','normalized');
y=y+1;
xlim([-nmesh^2 nmesh^2])
ylim([-nmesh^2 nmesh^2])
end
end
hold off
darova
on 24 Apr 2020
Is it possible for you to make a simple sketch like this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287129/image.png)
How do you imagine your result to look
sui zhi lau
on 25 Apr 2020
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287322/image.png)
So if possible, set a range to show how far the magnetic strength (for the above picture, it is x<=30 && y<=30) and each particle will move towards the centre incrementally (small jumps towards the centre) while the rest jumps randomly as usual.
darova
on 25 Apr 2020
Try this
c0 = nmesh; % center of magnetic field
for i = 1:size(dx,1)
[ii,jj] = ind2sub(size(X),i); % row and column of force
n = hypot(ii-c0,jj-c0); % distance to point
if n < 2.1 % radius of magnetic force
dx(i,:) = -cumsum(dx(i,:)*0+(jj-c0)/n); % cosinus
dy(i,:) = -cumsum(dy(i,:)*0+(ii-c0)/n); % sinus
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287353/image.png)
sui zhi lau
on 26 Apr 2020
Sorry but how am i supposed to put this in ?
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
%%
scale = 0.5;
y = 1;
nmesh = 8;
niter = 40;
[X,Y] = meshgrid(0:nmesh-1) % create mesh
X = 10*X;
Y = 10*Y;
dx = normrnd(0,1,[nmesh^2 niter]);
dy = normrnd(0,1,[nmesh^2 niter]);
cla
%plot(X(:),Y(:),'ob')% plot starting points
%%
grid off
hold on
%plot(X,Y,'k') % plot grid
%plot(X',Y','k')
%%
for i = 1:niter-1
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
q = quiver(X1, Y1, (dx(:,i+1)-dx(:,i)), (dy(:,i+1)-dy(:,i)),scale)
q.ShowArrowHead = 'on'
q.LineWidth = 3
% plot([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
% [Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]','.-r')
pause(0.5)
%% percentage readings
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('White Space = %0.3f%%',percentageofwhite(y)),'Units','normalized');
y=y+1;
xlim([-nmesh nmesh^2])
ylim([-nmesh nmesh^2])
end
hold off
sui zhi lau
on 26 Apr 2020
ya i define but i am still getting only the random moving ones ?
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
%%
scale = 0.5;
y = 1;
nmesh = 8;
niter = 40;
[X,Y] = meshgrid(0:nmesh-1) % create mesh
X = 10*X;
Y = 10*Y;
dx = normrnd(0,1,[nmesh^2 niter]);
dy = normrnd(0,1,[nmesh^2 niter]);
cla
%plot(X(:),Y(:),'ob')% plot starting points
%%
grid off
hold on
%plot(X,Y,'k') % plot grid
%plot(X',Y','k')
%%
c0 = nmesh; % center of magnetic field
for i = 1:size(dx,1)
[ii,jj] = ind2sub(size(X),i); % row and column of force
n = hypot(ii-c0,jj-c0); % distance to point
if n < 2.1 % radius of magnetic force
dx(i,:) = -cumsum(dx(i,:)*0+(jj-c0)/n); % cosinus
dy(i,:) = -cumsum(dy(i,:)*0+(ii-c0)/n); % sinus
end
plot([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
[Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]','.-r')
%% percentage readings
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('White Space = %0.3f%%',percentageofwhite(y)),'Units','normalized');
y=y+1;
xlim([-nmesh nmesh^2])
ylim([-nmesh nmesh^2])
end
hold off
sui zhi lau
on 27 Apr 2020
Edited: darova
on 27 Apr 2020
Im sorry Darova if i asked something ignorant. I only picked up MATLAB just late last year and I am still getting confused since most of what i did are self-learned.
Anyways, if possible, would you mind vetting through my 3D plot? I tried to change it up but I cant get it to run
Sorry for the trouble
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
niter = 40; % no. of jumps
scale = 0.5;
y = 1; % white percentage
%% range of results
d = 1;
nmesh = 4; %range
[X,Y,Z] = meshgrid((-nmesh+1):nmesh-1); % create mesh
X = X * d;
Y = Y * d;
Z = Z * d;
%% random direction for X,Y,Z
dx = normrnd(0,1,[(size(X,1))^3 niter]);
dy = normrnd(0,1,[(size(X,1))^3 niter]);
dz = normrnd(0,1,[(size(X,1))^3 niter]);
%% Magnetised particles
c0 = nmesh; % center of magnetic field
for i = 1:size(dx,1)
[ii,jj] = ind2sub(size(X),i); % row and column of force
n = hypot(ii-c0,jj-c0); % distance to point
if n < 3.5 % radius of magnetic force
dx(i,:) = -cumsum(dx(i,:)*0+(jj-c0)/n); % cosinus
dy(i,:) = -cumsum(dy(i,:)*0+(ii-c0)/n); % sinus
dz(i,:) = -cumsum(dz(i,:)*0+(ii-c0)/n);
end
end
cla
%plot3(X(:),Y(:),Z(:),'ob') % plot starting points
hold on
%plot(X,Y,'k') % plot grid
%plot(X',Y','k')
%% For Loop to plot graph
for i = 1:niter-1
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
Z1 = Z(:) + dz(:,i);
%q = quiver3(X1, Y1, Z1, dx(:,i+1)-dx(:,i), dy(:,i+1)-dy(:,i),dz(:,i+1)-dz(:,i))
%q.LineWidth = s;
%q.ShowArrowHead = 'off';
plot3([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
[Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]',...
[Z(:) Z(:)]'+[dz(:,i+1) dz(:,i)]','.-r')
pause(0.05)
%% percentage readings
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('White Space = %0.3f%%',percentageofwhite(y)),'Units','normalized');
y=y+1;
xlim([-5*d d*5])
ylim([-5*d d*5])
end
hold off
xlabel('My x label')
ylabel('My y label')
zlabel('My z label')
rotate3d on
sui zhi lau
on 28 Apr 2020
I tried it and it showed this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288146/image.png)
I dont see the random movements or the coverging to the centre.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
niter = 40; % no. of jumps
scale = 0.5;
y = 1; % white percentage
%% range of results
d = 1;
nmesh = 4; %range
[X,Y,Z] = meshgrid((-nmesh+1):nmesh-1); % create mesh
X = X * d;
Y = Y * d;
Z = Z * d;
%% random direction for X,Y,Z
dx = normrnd(0,1,[(size(X,1))^3 niter]);
dy = normrnd(0,1,[(size(X,1))^3 niter]);
dz = normrnd(0,1,[(size(X,1))^3 niter]);
%% Magnetised particles
c0 = nmesh; % center of magnetic field
for i = 1:size(dx,1)
[ii,jj,kk] = ind2sub(size(X),i); % row and column of force
n = pdist2([ii jj kk],[c0 c0 c0]); % distance to point
if n < 2.1 % radius of magnetic force
dx(i,:) = -cumsum(dx(i,:)*0+(ii-c0)/n); % cosinus
dy(i,:) = -cumsum(dy(i,:)*0+(jj-c0)/n); % sinus
dz(i,:) = -cumsum(dz(i,:)*0+(kk-c0)/n);
end
end
cla
%plot3(X(:),Y(:),Z(:),'ob') % plot starting points
hold on
%plot(X,Y,'k') % plot grid
%plot(X',Y','k')
%% For Loop to plot graph
for i = 1:niter-1
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
Z1 = Z(:) + dz(:,i);
%q = quiver3(X1, Y1, Z1, dx(:,i+1)-dx(:,i), dy(:,i+1)-dy(:,i),dz(:,i+1)-dz(:,i))
%q.LineWidth = s;
%q.ShowArrowHead = 'off';
plot3([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
[Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]',...
[Z(:) Z(:)]'+[dz(:,i+1) dz(:,i)]','.-r')
pause(0.001)
%% percentage readings
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('White Space = %0.3f%%',percentageofwhite(y)),'Units','normalized');
y=y+1;
xlim([-5*d d*5])
ylim([-5*d d*5])
end
hold off
xlabel('My x label')
ylabel('My y label')
zlabel('My z label')
rotate3d on
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)