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:
Creating a matrix of decreasing concentric rings of numbers

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Terence Britton

Date: 2 Dec, 2010 17:49:06

Message: 1 of 11

I am trying to create a matrix of numbers that decrease/increase as a function of their distance from the centre of the matrix in circular rings e.g.


3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3

Although this looks a little rectangle. My matrix needs to be 400x400 and to look as much like a set of concentric rings (rather than rectangle) as possible. I am just wondering if there is a function to write this. Maybe something where I could specify the starting central number and the final outer number and get matlab to fill the matrix appropriately with the inner numbers.

Any help with this would be greatly appreciated

Regards


Terence Britton

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Sean de

Date: 2 Dec, 2010 18:08:05

Message: 2 of 11

"Terence Britton" <terence.britton@student.manchester.ac.uk> wrote in message <id8m6i$7fg$1@fred.mathworks.com>...
> I am trying to create a matrix of numbers that decrease/increase as a function of their distance from the centre of the matrix in circular rings e.g.
>
>
> 3 3 3 3 3
> 3 2 2 2 3
> 3 2 1 2 3
> 3 2 2 2 3
> 3 3 3 3 3
>
> Although this looks a little rectangle. My matrix needs to be 400x400 and to look as much like a set of concentric rings (rather than rectangle) as possible. I am just wondering if there is a function to write this. Maybe something where I could specify the starting central number and the final outer number and get matlab to fill the matrix appropriately with the inner numbers.
>
> Any help with this would be greatly appreciated
>
> Regards
>
>
> Terence Britton

Two ways:

n = 400;
A1 = bsxfun(@(x,y)max(abs(x-ceil(n/2)),abs(y-ceil(n/2)))+1,1:n,(1:n).');

[xx yy] = meshgrid(1:n);
A2 = max(abs(xx-ceil(n/2)),abs(yy-ceil(n/2)))+1;

isequal(A1,A2)

Subject: Creating a matrix of decreasing concentric rings of numbers

From: ImageAnalyst

Date: 2 Dec, 2010 18:08:09

Message: 3 of 11

Terence Britton:
This is called the "Euclidean Distance Transform" and is performed by
the function bwdist() in the Image Processing Toolbox:

Here's an example for 11 by 11:

imageArray = zeros(11,11, 'uint8');
imageArray(6,6) = 1
[rows cols] = size(imageArray);
edm = bwdist(imageArray);

edm =

   7.0710678 6.4031243 5.8309517 5.3851647 5.0990195
5.0000000 5.0990195 5.3851647 5.8309517 6.4031243 7.0710678
   6.4031243 5.6568542 5.0000000 4.4721360 4.1231055
4.0000000 4.1231055 4.4721360 5.0000000 5.6568542 6.4031243
   5.8309517 5.0000000 4.2426405 3.6055512 3.1622777
3.0000000 3.1622777 3.6055512 4.2426405 5.0000000 5.8309517
   5.3851647 4.4721360 3.6055512 2.8284271 2.2360680
2.0000000 2.2360680 2.8284271 3.6055512 4.4721360 5.3851647
   5.0990195 4.1231055 3.1622777 2.2360680 1.4142135
1.0000000 1.4142135 2.2360680 3.1622777 4.1231055 5.0990195
   5.0000000 4.0000000 3.0000000 2.0000000 1.0000000
0 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
   5.0990195 4.1231055 3.1622777 2.2360680 1.4142135
1.0000000 1.4142135 2.2360680 3.1622777 4.1231055 5.0990195
   5.3851647 4.4721360 3.6055512 2.8284271 2.2360680
2.0000000 2.2360680 2.8284271 3.6055512 4.4721360 5.3851647
   5.8309517 5.0000000 4.2426405 3.6055512 3.1622777
3.0000000 3.1622777 3.6055512 4.2426405 5.0000000 5.8309517
   6.4031243 5.6568542 5.0000000 4.4721360 4.1231055
4.0000000 4.1231055 4.4721360 5.0000000 5.6568542 6.4031243
   7.0710678 6.4031243 5.8309517 5.3851647 5.0990195
5.0000000 5.0990195 5.3851647 5.8309517 6.4031243 7.0710678

Subject: Creating a matrix of decreasing concentric rings of numbers

From: ImageAnalyst

Date: 2 Dec, 2010 18:10:36

Message: 4 of 11

Sorry, the newsreader wrapped it weirdly, but it does give you the
distance from the center element for each element. The center is 0
though - not sure your rationale for saying the distance there is 1.
I guess you could add 1 to the result if you wanted that for some
reason.

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Sean de

Date: 2 Dec, 2010 18:13:04

Message: 5 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <354b12c4-4116-4fc9-a1dd-f3f483762d4b@r16g2000prh.googlegroups.com>...
> Terence Britton:
> This is called the "Euclidean Distance Transform" and is performed by
> the function bwdist() in the Image Processing Toolbox:
>
> Here's an example for 11 by 11:
>
> imageArray = zeros(11,11, 'uint8');
> imageArray(6,6) = 1
> [rows cols] = size(imageArray);
> edm = bwdist(imageArray);
>
> edm =
>
> 7.0710678 6.4031243 5.8309517 5.3851647 5.0990195
> 5.0000000 5.0990195 5.3851647 5.8309517 6.4031243 7.0710678
> 6.4031243 5.6568542 5.0000000 4.4721360 4.1231055
> 4.0000000 4.1231055 4.4721360 5.0000000 5.6568542 6.4031243
> 5.8309517 5.0000000 4.2426405 3.6055512 3.1622777
> 3.0000000 3.1622777 3.6055512 4.2426405 5.0000000 5.8309517
> 5.3851647 4.4721360 3.6055512 2.8284271 2.2360680
> 2.0000000 2.2360680 2.8284271 3.6055512 4.4721360 5.3851647
> 5.0990195 4.1231055 3.1622777 2.2360680 1.4142135
> 1.0000000 1.4142135 2.2360680 3.1622777 4.1231055 5.0990195
> 5.0000000 4.0000000 3.0000000 2.0000000 1.0000000
> 0 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
> 5.0990195 4.1231055 3.1622777 2.2360680 1.4142135
> 1.0000000 1.4142135 2.2360680 3.1622777 4.1231055 5.0990195
> 5.3851647 4.4721360 3.6055512 2.8284271 2.2360680
> 2.0000000 2.2360680 2.8284271 3.6055512 4.4721360 5.3851647
> 5.8309517 5.0000000 4.2426405 3.6055512 3.1622777
> 3.0000000 3.1622777 3.6055512 4.2426405 5.0000000 5.8309517
> 6.4031243 5.6568542 5.0000000 4.4721360 4.1231055
> 4.0000000 4.1231055 4.4721360 5.0000000 5.6568542 6.4031243
> 7.0710678 6.4031243 5.8309517 5.3851647 5.0990195
> 5.0000000 5.0990195 5.3851647 5.8309517 6.4031243 7.0710678

IA,

I thought he wanted the 'chessboard' distance metric?

Subject: Creating a matrix of decreasing concentric rings of numbers

From: ImageAnalyst

Date: 2 Dec, 2010 19:02:37

Message: 6 of 11

On Dec 2, 1:13 pm, "Sean de "
<sean.dewol...@nospamplease.umit.maine.edu> wrote:
> IA,
>
> I thought he wanted the 'chessboard' distance metric?
-------------------------------------------------------------------------------------
Sean:
I was confused by that too. That is certainly what he showed, but not
what he said when he said "their distance from the centre of the
matrix" so I gave what he said. Presumably you gave what he showed,
so he can take his pick.
ImageAnalyst

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Roger Stafford

Date: 2 Dec, 2010 20:00:23

Message: 7 of 11

"Terence Britton" <terence.britton@student.manchester.ac.uk> wrote in message <id8m6i$7fg$1@fred.mathworks.com>...
> I am trying to create a matrix of numbers that decrease/increase as a function of their distance from the centre of the matrix in circular rings e.g.
>
>
> 3 3 3 3 3
> 3 2 2 2 3
> 3 2 1 2 3
> 3 2 2 2 3
> 3 3 3 3 3
>
> Although this looks a little rectangle. My matrix needs to be 400x400 and to look as much like a set of concentric rings (rather than rectangle) as possible. I am just wondering if there is a function to write this. Maybe something where I could specify the starting central number and the final outer number and get matlab to fill the matrix appropriately with the inner numbers.
>
> Any help with this would be greatly appreciated
> Regards
> Terence Britton
- - - - - - - - -
  If S and F are your starting and final values at the center and corners of the n x n matrix, respectively, then do this:

 n = 400;
 [I,J] = meshgrid(1:n,1:n);
 M = round(S+(F-S)/(n-1)*sqrt(2*((I-(n+1)/2).^2+(J-(n+1)/2).^2)));

  (Note: If n is even, the F value itself may not appear since there is no central point.)

Roger Stafford

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Roger Stafford

Date: 2 Dec, 2010 20:09:05

Message: 8 of 11

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <id8tsn$rg$1@fred.mathworks.com>...
> ......
> (Note: If n is even, the F value itself may not appear since there is no central point.)
> ......
- - - - - - - - - -
  I meant to say: If n is even, the S value itself may not appear since there is no central point.

Roger Stafford

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Terence Britton

Date: 2 Dec, 2010 23:32:04

Message: 9 of 11

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <id8ud1$458$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <id8tsn$rg$1@fred.mathworks.com>...
> > ......
> > (Note: If n is even, the F value itself may not appear since there is no central point.)
> > ......
> - - - - - - - - - -
> I meant to say: If n is even, the S value itself may not appear since there is no central point.
>
> Roger Stafford

Thanks Roder this is perfect

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Terence Britton

Date: 2 Dec, 2010 23:36:06

Message: 10 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a1213236-6b58-40b9-92b1-153f0598dd44@o11g2000prf.googlegroups.com>...
> On Dec 2, 1:13 pm, "Sean de "
> <sean.dewol...@nospamplease.umit.maine.edu> wrote:
> > IA,
> >
> > I thought he wanted the 'chessboard' distance metric?
> -------------------------------------------------------------------------------------
> Sean:
> I was confused by that too. That is certainly what he showed, but not
> what he said when he said "their distance from the centre of the
> matrix" so I gave what he said. Presumably you gave what he showed,
> so he can take his pick.
> ImageAnalyst

I actually said "as a function of their distance from the centre of the matrix" not the actual distance. Thanks though, that operation will be very useful at some point.

Subject: Creating a matrix of decreasing concentric rings of numbers

From: Terence Britton

Date: 2 Dec, 2010 23:42:04

Message: 11 of 11

"Terence Britton" <terence.britton@student.manchester.ac.uk> wrote in message <id9a9k$12p$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <id8ud1$458$1@fred.mathworks.com>...
> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <id8tsn$rg$1@fred.mathworks.com>...
> > > ......
> > > (Note: If n is even, the F value itself may not appear since there is no central point.)
> > > ......
> > - - - - - - - - - -
> > I meant to say: If n is even, the S value itself may not appear since there is no central point.
> >
> > Roger Stafford
>
> Thanks Roder this is perfect


Apologies... *Roger

Tags for this Thread

No tags are associated with 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