NEED HELP SOLVING "Array indices must be positive integers or logical values"

1 次查看(过去 30 天)
Im looking at a 3x3 grid around specific number within a larger 16x16 and in some cases i land up being outside of my 16x16 grind and therefore getting and error. Want i want to do is say if outside 16x16 grind then =0.
%new attempt (want to say if outside box then =0)
for i=a(1):a(256) %trying to calcule neighbours around each cell containing one
c=[a(i+1),a(i-1),a(i+16),a(i-16),a(i+15),a(i+17),a(-15),a(-17)]
c(isnan(c))=0
A(i)=c; %stores value as vector
end

采纳的回答

David Hill
David Hill 2020-8-29
a=blkdiag(0,a,0);
count=1;
for m=2:17
for n=2:17
A{count}=[a(m+1,n),a(m-1,n),a(m,n+1),a(m,n-1),a(m-1,n+1),a(m+1,n+1),a(m+1,n-1),a(m-1,n-1)];
count=count+1;
end
end
  2 个评论
Daniel Hodgson
Daniel Hodgson 2020-8-29
编辑:Daniel Hodgson 2020-8-29
Do i understand this correctly if saying a=blkdiag(0,a,0) just creates a sorround box with zeros? Then m,n being directions (x,y). What does count do in this case? And also i would like to store the anwsers in a vector how would this be done in this case?
Im a beginner so as much clarification as possible really does help.
David Hill
David Hill 2020-8-30
If you do not want a cell array, you can combine into a matrix. Count just counts the total in both loops (16*16=256 total rows or cells).
a=blkdiag(0,a,0);
count=1;
for m=2:17
for n=2:17
A(count,:)=[a(m+1,n),a(m-1,n),a(m,n+1),a(m,n-1),a(m-1,n+1),a(m+1,n+1),a(m+1,n-1),a(m-1,n-1)];
count=count+1;
end
end
The above makes a matrix instead of a cell. Each row of the matrix corresponds to a vector. A(1,:) is the vector of the first point, A(2,:) is the vector of the second point, ... You could change the loops if you want to change the order.
a=blkdiag(0,a,0);
count=1;
for n=2:17%run rows second
for m=2:17%run columns first
A(count,:)=[a(m+1,n),a(m-1,n),a(m,n+1),a(m,n-1),a(m-1,n+1),a(m+1,n+1),a(m+1,n-1),a(m-1,n-1)];
count=count+1;
end
end

请先登录,再进行评论。

更多回答(1 个)

Peter O
Peter O 2020-8-29
Does it need to be a one-liner? If you can grab the x,y coordinate, you could clamp your index points to the box bounds using
% For a 256 x 256 grid
x_bounds = min(256, max(1, [index_x-16, index_x+16]))
y_bounds = min(256, max(1, [index_y-16, index_y+16]))
%Then get the 16x16 (or smaller) blob:
M = A(x_bounds(1):x_bounds(2),y_bounds(1):y_bounds(2))
You could do the same to clamp the 3x3 if it's near the edge of the image.
  2 个评论
Daniel Hodgson
Daniel Hodgson 2020-8-29
No it doesnt need to be a one liner, this would probally be perfect. Can you please explain how the functions work more speficaly what each component does (Im new to matlab and only know the basics), this part is quite unclear to me "max(1, [index_x-16, index_x+16]) "? I have looked up the functions and they seem to rearrange the order of the numbers. It is for a school project where i need to program Conway's Game Of Life so therefore I mainly seeking knowledge and tips.
Peter O
Peter O 2020-8-30
Sorry for the delay and I see you've found a solution that works, and I think I misinterpreted your question at first because my solution wasn't what you wanted to do. However, to close the loop here:
Assuming you have a grid of 256x256 and you have an (x,y) position indictated by (index_x, index_y), this code will extract the square with a side length of 33 pixels around it and the (index_x,index_y) point at its center (16 pixels to either side). The minmax wrapping ensures that you never exceed the bounds of the grid 1...256. If you access something where the (index_x, index_y) is under 16 pixels from an edge, it clamps to the edge so you don't get an out-of-bounds error. If you were 8 pixels from the left edge and in the middle of the grid vertically, for instance (x,y) is (8, 50), you'd get a 24x33 region returned.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by