Thread Subject:
Averaging large Matrix using blocks

Subject: Averaging large Matrix using blocks

From: Matthew Balint

Date: 1 Aug, 2013 14:37:17

Message: 1 of 5

Hello everyone,

I am a MATLAB newbie so go easy.

   I have a 400x400 matrix that I am attempting to average by taking 4x4 blocks, starting in the top left corner of my array and moving to the right/down. From each block, I need a single point (the mean) that will be used, at the end, in conjunction with all of the other mean values, to make a contour plot. When going from one 4x4 block to the next, no overlapping would be essential.

After 4x4 blocks, I need to do 9x9 blocks, then 16x16 blocks.

If this can be done without loops, that would be great.

As stated above, I am really new to this stuff so any help to get my feet off the ground would be great! Thanks a lot in advance.

-Matt

Subject: Averaging large Matrix using blocks

From: Nasser M. Abbasi

Date: 1 Aug, 2013 14:59:13

Message: 2 of 5

On 8/1/2013 9:37 AM, Matthew Balint wrote:
> Hello everyone,
>
> I am a MATLAB newbie so go easy.
>
> I have a 400x400 matrix that I am attempting to average by taking 4x4 blocks,
> starting in the top left corner of my array and moving to the right/down.
> From each block, I need a single point (the mean) that will be used, at the end,

Here is for 4x4 example

--------------------------
A=randi(6,6)

      6 2 3 1 3 2
      6 5 1 1 3 6
      3 6 2 2 4 3
      6 6 1 2 4 5
      3 4 6 6 3 2
      1 6 3 5 6 1

filter=1/4*ones(2);
conv2(A,filter,'full')

     1.5000 2.0000 1.2500 1.0000 1.0000 1.2500 0.5000
     3.0000 4.7500 2.7500 1.5000 2.0000 3.5000 2.0000
     2.2500 5.0000 3.5000 1.5000 2.5000 4.0000 2.2500
     2.2500 5.2500 3.7500 1.7500 3.0000 4.0000 2.0000
     2.2500 4.7500 4.2500 3.7500 3.7500 3.5000 1.7500
     1.0000 3.5000 4.7500 5.0000 5.0000 3.0000 0.7500
     0.2500 1.7500 2.2500 2.0000 2.7500 1.7500 0.2500

You can extend it to other sizes of blocks. help conv2.


>in conjunction with all of the other mean values, to make a contour plot.
>When going from one 4x4 block to the next, no overlapping would be essential.
>
> After 4x4 blocks, I need to do 9x9 blocks, then 16x16 blocks.
>
> If this can be done without loops, that would be great.
>
> As stated above, I am really new to this stuff so any help to get my feet
>off the ground would be great! Thanks a lot in advance.
>
> -Matt
>

Subject: Averaging large Matrix using blocks

From: Nasser M. Abbasi

Date: 1 Aug, 2013 15:08:57

Message: 3 of 5

>
> Here is for 4x4 example

opps, meant 2x2 example...

>
> --------------------------
> A=randi(6,6)
> filter=1/4*ones(2);
> conv2(A,filter,'full')

Subject: Averaging large Matrix using blocks

From: Matthew Balint

Date: 1 Aug, 2013 15:47:07

Message: 4 of 5

"Nasser M. Abbasi" wrote in message <ktdt3t$6k6$1@speranza.aioe.org>...
> On 8/1/2013 9:37 AM, Matthew Balint wrote:
> > Hello everyone,
> >
> > I am a MATLAB newbie so go easy.
> >
> > I have a 400x400 matrix that I am attempting to average by taking 4x4 blocks,
> > starting in the top left corner of my array and moving to the right/down.
> > From each block, I need a single point (the mean) that will be used, at the end,
>
> Here is for 4x4 example
>
> --------------------------
> A=randi(6,6)
>
> 6 2 3 1 3 2
> 6 5 1 1 3 6
> 3 6 2 2 4 3
> 6 6 1 2 4 5
> 3 4 6 6 3 2
> 1 6 3 5 6 1
>
> filter=1/4*ones(2);
> conv2(A,filter,'full')
>
> 1.5000 2.0000 1.2500 1.0000 1.0000 1.2500 0.5000
> 3.0000 4.7500 2.7500 1.5000 2.0000 3.5000 2.0000
> 2.2500 5.0000 3.5000 1.5000 2.5000 4.0000 2.2500
> 2.2500 5.2500 3.7500 1.7500 3.0000 4.0000 2.0000
> 2.2500 4.7500 4.2500 3.7500 3.7500 3.5000 1.7500
> 1.0000 3.5000 4.7500 5.0000 5.0000 3.0000 0.7500
> 0.2500 1.7500 2.2500 2.0000 2.7500 1.7500 0.2500
>
> You can extend it to other sizes of blocks. help conv2.
>
>
> >in conjunction with all of the other mean values, to make a contour plot.
> >When going from one 4x4 block to the next, no overlapping would be essential.
> >
> > After 4x4 blocks, I need to do 9x9 blocks, then 16x16 blocks.
> >
> > If this can be done without loops, that would be great.
> >
> > As stated above, I am really new to this stuff so any help to get my feet
> >off the ground would be great! Thanks a lot in advance.
> >
> > -Matt
> >


Hmm. Here is simplified code to better demonstrate what I mean:
where layer6 is my 400x400 matrix...


X=layer6(200:203,250:253);
   %get my 4x4 matrix, (random here for all intents and purposes)

T=sum(X);
    
S=mean(T);
  %get my single mean value of the 4x4 matrix

This is what I want to do, starting from 1,1 of my matrix. Then write a loop that will get the 4x4 blocks until there are none left out of my matrix. Does that better illustrate what I mean? Thanks again for your help!

Subject: Averaging large Matrix using blocks

From: Nasser M. Abbasi

Date: 1 Aug, 2013 16:38:57

Message: 5 of 5

On 8/1/2013 10:47 AM, Matthew Balint wrote:

>
> Hmm. Here is simplified code to better demonstrate what I mean:
> where layer6 is my 400x400 matrix...
>
>
> X=layer6(200:203,250:253);
> %get my 4x4 matrix, (random here for all intents and purposes)
>
> T=sum(X);
>
> S=mean(T);
> %get my single mean value of the 4x4 matrix

No

EDU>> layer6=rand(1000);
EDU>> X=layer6(200:203,250:253);
EDU>> T=sum(X);
EDU>> S=mean(T)

     1.9922

EDU>> mean(X(:))

     0.4981

Not the same. You can't sum then take the mean of the sum, since
you'll be dividing by 4 when taking the mean, instead of 16.

EDU>> length(X(:))
     16

>
> This is what I want to do, starting from 1,1 of my matrix. Then write a loop
>that will get the 4x4 blocks until there are none left out of my matrix.
>Does that better illustrate what I mean? Thanks again for your help!
>

what you asking for is easy in matlab, really have to go now.

--Nasser

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us