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?
5 次查看(过去 30 天)
显示 更早的评论
sui zhi lau
2020-4-7
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 个评论
darova
2020-4-7
Can you be more specific? Here is the result of one of your script
How do you want it to look like?
sui zhi lau
2020-4-7
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:
But with the same moving plot for each points on this plot.
采纳的回答
darova
2020-4-7
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 个评论
sui zhi lau
2020-4-7
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
2020-4-7
- 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?
sui zhi lau
2020-4-7
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):
darova
2020-4-7
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
2020-4-8
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
2020-4-8
It's because of quiver scale. If you uncomment this part
You will see the actual movement of your particles
Try to scale quiver
scale = 0.5;
quiver(X1, Y1, dx(:,i+1)-dx(:,i), dy(:,i+1)-dy(:,i),scale)
sui zhi lau
2020-4-8
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
2020-4-8
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.
So still something like this but the arrows are moving in 3 directions (x,y and z coordinates)
darova
2020-4-8
What if just add
dz = normrnd(0,1,[nmesh^2 niter]);
%
Z1 = 0 + dz(:,i);
And use quiver3?
sui zhi lau
2020-4-8
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
2020-4-24
编辑:sui zhi lau
2020-4-24
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.
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
2020-4-24
Is it possible for you to make a simple sketch like this?
How do you imagine your result to look
sui zhi lau
2020-4-25
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
2020-4-25
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
sui zhi lau
2020-4-26
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
2020-4-26
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
2020-4-27
编辑:darova
2020-4-27
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
2020-4-28
I tried it and it showed this
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)