"Emma Robertson" wrote in message <ipp3j8$5pa$1@fred.mathworks.com>...
> Hi everyone,
>
> Does anyone know of a neat method of finding the position of matrix elements (nonzeros, in this case) that are located around the edge?
>
> Say, I have a matrix A
>
> A=[
> 0 0 0 0 0 0
> 0 1 1 1 1 0
> 0 0 1 1 0 0
> 0 1 1 1 1 0
> 0 0 0 0 0 0]
>
> I want to find the position of the 1's that are located near to a zero. You can treat the zeros as background and the 1's as the desired object.
>
> I know that you can find where all the 1's are and find whether the neighbouring element is zero and if so you can regard the '1' as part of the edge.
> But, I'm dealing with a large matrix and I'm a bit reluctant to do it this way as it would be very costly to run through each element and its neighbour. Any suggestions of a more efficient way?
>
> I hope I have managed to explain the problem clearly.
> Many thanks!
Hello,
I'm not sure I understand. In your example, would all ones be part of the edge, as they all are neighbours of at least one zero ?
You could try this:
>ind=A==0;
>A(ind)=Inf;
>k1=[1,1,1;1,0,1;1,1,1]/8;
>B=conv2(A,k1,'same');
>~isnan(B).*~isinf(B)
Regards,
Florin
