Alternate to the circshift function in a 3d matix for an agent based model

3 visualizaciones (últimos 30 días)
Hey there,
I have a 3 dimensional matrix randworld and need to perform certain calculations on each the row and column cell around a randomly selected cell holding the third dimension constant. I used the circshift function for this. However as I need to do 121000000 iterations, this method is very slow. I noticed the circshift function consumes most of the time (from profile viewer). Could you please suggest an alternative to the circshift function.
I have copied the code below so that you have an idea of what I'm looking to do.
Thank you.
nside=11; %rows and coloumns
u=120; %neighbours
randworld=randi(10,nside,nside,5); %create a random 3-d world
%select a random agent and feature
randomagentr=randi(10,1);
randomagentc=randi(10,1);
randomfeat=randi(5,1);
nsize=0;
%neighbourhood
for q=-5:5
for w=-5:5
if abs(q)+abs(w)~=0
nsize=nsize+1;
neigh(nsize,:)=[q,w];
end
end
end
for k=1:u
vm=circshift(randworld,[neigh(k,1),neigh(k,2)]);
v=vm(randomagentr,randomagentc,randomfeat);
%perform certain computations on this new variable v
end

Respuesta aceptada

Roger Stafford
Roger Stafford el 29 de Ag. de 2013
If a circular shift by +1 done with indexing is significantly faster than your 'circshift' operations, then you can do the equivalent of your circular shifts in the last nested for-loops as a succession of such "index" shifts by +1 and it would then presumably be faster. Don't create the array 'neigh'. Just do this:
vm = circshift(squeeze(randworld(:,:,randomfeat)),[-5,-5]);
p = [11,1:10]; % <-- Another way to do circular shift by +1
for ir = 1:11
for ic = 1:11
v=vm(randomagentr,randomagentc);
%perform certain computations on this new variable v
vm = vm(:,p); % Circular column shift by +1
end
vm = vm(p,:); % Circular row shift by +1
end
You will have to test for the case ir == 6 and ic == 6 to avoid the case when there is to be no overall shift in order to get just 120 instead of 121 steps.

Más respuestas (0)

Categorías

Más información sobre Training and Simulation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by