Azzera filtri
Azzera filtri

Selecting the maximum value of a cell within a specific range, x number of times?

1 visualizzazione (ultimi 30 giorni)
Hi,
I'm new to matlab but will be using this to build an agent based model. I have a matrix (M), and a starting position (xpos=5 , ypos=5). I need to select the cell with the maximum value within a distance of 3 cells in every direction of the starting position, and repeat this 10 times. I am using a reference code, but I can't seem to figure out what the following two lines of code mean within the larger script:
distance = sqrt((ystart+ii-5)^2 + (xstart+jj-5)^2);
if M(ystart + ii,xstart + jj) > maxevi
I know this is the formula for the distance between two points, but I'm not sure why you have to subtract the "5", which is the starting position in the matrix. Here is the full code to select the highest value within a certain distance, 10 times:
for i=1:maxtime %maxtime would be 10
xstart=xpos-range %range would be 3 cells
ystart=ypos-range
max=0 %this sets the maximum value to 0
maxx=xstart
maxy=ystart
for jj=1:6
for ii=1:6 %to cover the range of 3 for all directions of the starting position each time
distance = sqrt((ystart+ii-5)^2 + (xstart+jj-5)^2);
if M(ystart + ii,xstart + jj) > maxevi
maxevi = M(ystart + ii,xstart + jj);
maxx = xstart + jj;
maxy = ystart + ii;
end
end
end

Risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by