I have a simple Matlab program that uses a set of random number lists and runs a series of trials using those numbers. Right now, the trials are run iteratively using the code below. How could this code be modified to eliminate the need for that iterative step? The program would be a lot more efficient if it could be properly vectorized.
size = 1000;
trials = 1000;
grid = zeros(size,size);
rx1 = randi(size,trials,1);
ry1 = randi(size,trials,1);
rx2 = randi(size,trials,1);
ry2 = randi(size,trials,1);
xmin = min(rx1,rx2);
xmax = max(rx1,rx2);
ymin = min(ry1,ry2);
ymax = max(ry1,ry2);
%This is the loop that I want to eliminate
for n=1:trials;
grid(ymin(n):ymax(n),xmin(n):xmax(n)) = grid(ymin(n):ymax(n),xmin(n):xmax(n)) + 1;
end
figure
mesh(grid);
I would use a trick inspired by integral images:
is equivalent to:
So for your problem I would do:
I’ve tested it on my laptop. Results are same: