encontrar vecinos a la distancia d en matrix + matlab

I want all neighbors of a point coordinate in a matrix at a distance d from it along with that point.

What is the most efficient way to generate such a sub-matrix ?

Por ejemplo si A = [ 1 , 2 , 3 ; 4 , 5 , 6 ; 7 ,8 ,9]

entonces para d = 0 and data 5 i would get the answer as 5 pero para los d = 1 i will get the matrix A as the result since the size of A itself is 3X3.

At the same time if the index does not exist in the matrix then i should get 0 at that point.

entonces para data point 2 and d = 1 la respuesta seria [ 0 , 0 , 0 ; 1 , 2 ,3 ; 4 , 5 , 6]

Cómo puedo hacerlo ?

preguntado el 09 de marzo de 12 a las 13:03

3 Respuestas

Try this out ...

A = padarray( A , [d d] )
% assuming the data is at r row and c col do the following

A( r : r + 2d , c : c + 2 d )

I hope it works ... did'nt really check !!

respondido 09 mar '12, 17:03

octave does not know padarray :-( but nice if it works so compact - kyss tao

Here's a solution. I'll leave you to turn it into a function. Let n denote the value at the centre of your sub-array, which is 5 en tu ejemplo.

z = zeros(size(A,1)+2*d,size(A,2)+2*d);
z(d+1:d+size(A,1),d+1:d+size(A,2)) = A;
[r,c] = find(z==n);
z(r-d:r+d,c-d:c+d)

EDITAR

Try this version, which is as inadequately tested as my earlier attempt. This still won't cope if the find expression returns multiple locations.

And I still make no warranties as to the efficiency of this.

respondido 09 mar '12, 15:03

case n=2,d=1, (=> r=1) gives r-d=0 => zero subindex - kyss tao

It is incorrect... i can assume that i know r,c but it is still giving me wrong answer in terms of neighbours - anon

I took the following matrix 'A = [ 1 , 2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]' and gave r = 2 and c=2 with d = 1 ... it returns me ' 0 0 0 ; 0 1 2 ; 0 5 6' - anon

So, now over to you to fix either my offering or @Kyss's. - Marca de alto rendimiento

To start with the case that you are inside A, instead of giving d=0 y data_point=5 deberías dar data_point=[2,2]. If you have it given as 5 you can convert by

ij = [ceil(data_point/size(A,1)), rem(data_point-1, size(A,1))+1]

or use find, as in High's answer, depending how to interpret your question.

La siguiente función

function B = find_neigh(A,ij,d)
imin = max(ij(1)-d,1);
imax = min(ij(1)+d,size(A,1));
jmin = max(ij(2)-d,1);
jmax = min(ij(2)+d,size(A,2));
B = zeros(2*d+1,2*d+1);
r1 = max(2-ij(1)+d,1);
c1 = max(2-ij(2)+d,1);
B(r1:r1+imax-imin, c1:c1+jmax-jmin) = A(imin:imax,jmin:jmax);

should return you the desired result. Call it for example with

A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
find_neigh(A,[1,2],1)

Edit: fixed two, no three bugs, and made it a function

respondido 09 mar '12, 18:03

you mean with regards to my fixes, or did you find one more mistake? - kyss tao

No @Kyss, wrt your original post, I've not tested your code at all. Crikey, I've hardly tested mine. - Marca de alto rendimiento

Could you give a working example ? I can have it as data ie i know where the data point is.... The answer given by @ High Performance Mark unfortunately did'nt work.... - anon

ok, I put the code into a function now and gave an example how to call it. Save the function as find_neigh.m. Have fun! - kyss tao

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.