0

I was wondering if some can help me regarding the row-column looping.

I have my row, col positions as [row,col]. e.g. array 300X2

I am trying to find their 8 neighbour values as:

neighbors_id= [matrix_cell_id(row, col),...
    matrix_cell_id(row-1, col-1),...
    matrix_cell_id(row-1, col),...
    matrix_cell_id(row-1, col+1),...
    matrix_cell_id(row, col-1),...
    matrix_cell_id(row, col+1),...
    matrix_cell_id(row+1, col-1),...
    matrix_cell_id(row+1, col),...
    matrix_cell_id(row+1, col+1)];

however when I run this code I get multiple values (matrix 300*2700) instead of 300row x 9 columns (corresponding to those neighbors) array.

Any clue is more than welcome,

Thanks a lot,

Marcos
  • 1
  • 1

1 Answers1

1

If row and col are 300x1 vectors, the call to

matrix_cell_id(row, col)

will return a 300x300 submatrix with the specified rows and columns. If you need the particular 300x1 elements indexed by row and col, you can use sub2ind:

matrix_cell_id(sub2ind(size(matrix_cell_id), row, col))
Iban Cereijo
  • 1,675
  • 1
  • 15
  • 28
  • Thanks a lot for this trick! I also applied this to matrix_area with same size as matrix_cell_id. Do you know how I can search through each of the rows and compare it with another array. i.e. compare each of these 9 numbers with another number from array (300x1) and take the closest value and its id? Thansk a lot – Marcos Feb 08 '17 at 17:46