Hello and thank you for taking the time to read my question,
I have a matrix of randomly generated boolean values, what I want to do is compare each individual cell of the matrix to its surroundings, by surroundings I mean the cells to the immediate left, right, top, bottom, and all 4 diagonals. If a surrounding cell is true I need it to return a “1”, if false, a “0”, so that I may add the number of true and false cells surrounding an individual cell, so an output would be something like “1 + 1 + 1 + 0 etc”. Im very sorry if I have explained this poorly, if more information in needed please let me know.
The simplest way to implement this is to nest two
forloops, one for x and one for y, with an array of the values [-1, 0, 1]. Make sure to account for boundaries of the matrix, and don’t add the value of the current cell.One note: this would be a good place to split your code into two methods, one doing the looping and calling the other with coordinates, while the other performs edge-case checking and returns the result.