so let say i have a 4x4 matric...now lets consider coordinates 0,0..the first cell...now just outside the matrix..in the left angle..i mean 0-1, 0-1, i.e. -1,-1..can i refer the outside of the matrix like that? i want to find a way to make my movement not go out of the matrix...
ok so now to make it more clear am gonna post the whole question...i have a matrix of eg. 4x4..and if given a coordinate..i must calculate the mean of the values surronding that coordinate minus the value of the coordinate itself... so if i have the coordinate to be 0,0...only the right down part must be calculated since the left up part is outside the matrix...same story if i am given the coordinate cell 3,3..practically all the cells sitting on the borders will have something going out of the matrix which must be taken care of in some way some how...any help? been fussing over this for hours...HELP!...thanks
I've been trying all night and am not getting it thats why am here...if you wont help just move on...I surely know the code am going to post is wrong because is not giving me the desired result...and i can see that is not something too big..just that my head is cloudy and am not getting it..because i can do the sudoku, and i can also do all the submatrixes thing...but going outside the matrix in this way is new to me...so if you wont help pls just move on...thanks
Assume the matrix is nx*ny. Then your function should look like this (assuming no syntax errors):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
double average(double **mat, int nx, int ny, int i, int j)
{
int neighbours;
double av=0.;
for(int ix=i-1;ix<=i+1;ix++)
for(int jy=j-1;jy<=j+1;jy++)
{
if((ix>=0)&&(ix<nx)&&(jy>=0)&&(jy<ny))
{
av+=mat[ix][jy];
neighbours++;
}
}
// the above cycle adds mat[i][j] as well
av-=mat[i][j];
av/=(neighbours-1);
return av;
}