現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)