Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
position of outer matrix elements

Subject: position of outer matrix elements

From: Emma Robertson

Date: 3 May, 2011 14:31:04

Message: 1 of 3

Hi everyone,

Does anyone know of a neat method of finding the position of matrix elements (non-zeros, 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!

Subject: position of outer matrix elements

From: Florin Neacsu

Date: 3 May, 2011 17:21:05

Message: 2 of 3

"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 (non-zeros, 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

Subject: position of outer matrix elements

From: Emma Robertson

Date: 4 May, 2011 11:08:04

Message: 3 of 3

"Florin Neacsu" wrote in message <ippdi1$5h3$1@fred.mathworks.com>...
> "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 (non-zeros, 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

Thanks alot!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us