Get grayscale like image from 2D representation of 3D random plot
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone I have the next code that creates multiple 3D random walks from random points and gives images like these:
and if I take the 2D representation(with view(2)) of the above 3D plot and make all the colors black I get images like these :
but is there a way to get a grayscale like image in the sense that if one point has multiple points beneath it in the 3D space it get darker and darker while if it has fewer or no point below it in the 3D space it is brighter??
My code is the following:
lambda = 3; %Mean free path
numberOfSteps = 10000; % Total number of steps
numberOfPaths = 3; % Number of paths
x(1,:) = rand(1,numberOfPaths); % Initial position (x)
y(1,:) = rand(1,numberOfPaths); % Initial position (y)
z(1,:) = rand(1,numberOfPaths); % Initial position (z)
for j = 1:numberOfPaths
for i = 1:numberOfSteps
r = -lambda*log(rand()); % Distance Travelled
theta = pi*rand(); % Arbritary angle in between 0 and Pi
phi = 2*pi*rand(); % Arbritary angle in between 0 and 2Pi
dx = r*sin(theta)*cos(phi); % Step Size (x)
dy = r*sin(theta)*sin(phi); % Step Size (y)
dz = r*cos(theta); % Step Size (z)
x(i+1,j) = x(i,j) + dx; % Position at the end of the first step (x)
y(i+1,j) = y(i,j) + dy; % Position at the end of the second step (y)
z(i+1,j) = z(i,j) + dz; % Position at the end of the third step (z)
end
end
plot3(x(:,1), y(:,1), z(:,1), 'k');
hold on
plot3(x(:,2), y(:,2), z(:,2), 'r');
plot3(x(:,3), y(:,3), z(:,3), 'g');
hold off
%view(2);
%axis off;
0 个评论
采纳的回答
Bjorn Gustavsson
2022-12-8
Have a look at the help and documentation of histcounts2. That function will allow you to calculate this type of cumulative counts. You simply have to concatenate the x and y-coordinates of all trajectories. Perhaps something like this:
x_all = [r_1(:,1);r_2(:,1);r_3(:,1),.. ,r_n(:,1)]; % somehow just make column-arrays of
y_all = [r_1(:,2);r_2(:,2);r_3(:,2),.. ,r_n(:,2)]; % all x and y-coordinates from all trajectories
n_x = 97;
n_y = 103;
x_lims = linspace(min(x_all),max(x_all),n_x);
y_lims = linspace(min(y_all),max(y_all),n_y);
[N_XY] = histcounts2(x_all,y_all,x_lims,y_lims);
imagesc(x_lims,y_lims,N_XY),colormap(gray) % Perhaps N_XY should be permuted here, you check
HTH
3 个评论
Torsten
2022-12-9
编辑:Torsten
2022-12-9
lambda = 3; %Mean free path
numberOfSteps = 100000; % Total number of steps
numberOfPaths = 3; % Number of paths
x(1,:) = rand(1,numberOfPaths); % Initial position (x)
y(1,:) = rand(1,numberOfPaths); % Initial position (y)
z(1,:) = rand(1,numberOfPaths); % Initial position (z)
for j = 1:numberOfPaths
for i = 1:numberOfSteps
r = -lambda*log(rand()); % Distance Travelled
theta = pi*rand(); % Arbritary angle in between 0 and Pi
phi = 2*pi*rand(); % Arbritary angle in between 0 and 2Pi
dx = r*sin(theta)*cos(phi); % Step Size (x)
dy = r*sin(theta)*sin(phi); % Step Size (y)
dz = r*cos(theta); % Step Size (z)
x(i+1,j) = x(i,j) + dx; % Position at the end of the first step (x)
y(i+1,j) = y(i,j) + dy; % Position at the end of the second step (y)
z(i+1,j) = z(i,j) + dz; % Position at the end of the third step (z)
end
end
%Project all points in the x-y plane
x_all = x(:);
y_all = y(:);
n_x = 150;
n_y = 150;
x_lims = linspace(min(x_all),max(x_all),n_x);
y_lims = linspace(min(y_all),max(y_all),n_y);
[N_XY] = histcounts2(x_all,y_all,x_lims,y_lims);
imagesc(x_lims,y_lims,N_XY),colormap(flipud(gray) )
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!