Help me make my code faster
Show older comments
I'm making a grid of 3D positions and using colour to express the number of particles/agents/whatever at that position. Currently I'm using 3 for-loops to cycle through each point and find how many particles at that position. Is there a more MATLAB way to do this using indices or quicker functions? Full code attached, important bit below:
% lim = 6
% LIMS = -lim:lim
[xm,ym,zm] = meshgrid(LIMS,LIMS,LIMS);
density = ones(2*lim+1,2*lim+1,2*lim+1); % 2n+1 to capture entirety of LIMS. (-6 to 6 spans 2*6+1 points)
for a = [10 20] % outermost for-loop because I want to capture probability at two separate time steps, namely t=10 and t=20.
for i = LIMS
for j = LIMS
for k = LIMS
% i+lim+1 translates -6:6 -> 1:13
density(i+lim+1,j+lim+1,k+lim+1) = sum(ismember(A(:,:,a),[i j k],'rows'));
% ^^ this ^^ bit counts how many agents, from A, have that exact position.
% A is 100000 x 3, with 100 sheets (one per timestep).
% That's 100,000 agents, where the first column is x dimension, second is y, third is z of course.
end
end
end
figure; slice(xm,ym,zm,density,0,0,0);
colormap('hot'); grid minor;
end
The full story is that I'm trying to simulate 100,000 agents starting at the origin and propagating outwards in 1,2,3,4, ... dimensions, with an equal probability of moving in any one direction. This'll just help me visualise this distribution in 3D.
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!