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:
Averaging large Matrix using blocks

Subject: Averaging large Matrix using blocks

From: Matthew Balint

Date: 1 Aug, 2013 14:37:17

Message: 1 of 8

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 8

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 8

>
> 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 8

"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 8

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

Subject: Averaging large Matrix using blocks

From: Nasser M. Abbasi

Date: 2 Aug, 2013 03:06:28

Message: 6 of 8

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


try this. There is probably a way to do this with conv2 but
too lazy to find out now:

-------------------
% takes means of blocks of submatrices from larger matrix

N = 8; % size of main matrix
A = randi(N,N);
M = 4; %blocks, must be mod zero with N
r = zeros(N^2/M^2,1); % collect results
k = 0;

for i=1:M:N
     for j=1:M:N
         k = k+1;
         r(k) = mean(mean(A(i:i+M-1,j:j+M-1)));
     end
end
---------------------------------

A =
      4 3 2 4 7 4 8 2
      5 8 3 8 2 4 1 7
      2 6 8 4 2 7 4 3
      4 6 6 1 2 2 3 2
      3 8 1 3 7 2 4 4
      4 8 2 7 1 4 4 5
      1 3 7 2 6 1 5 6
      3 2 5 8 2 2 3 7

EDU>> r
     4.6250
     3.7500
     4.1875
     3.9375

EDU>> mean(mean(A(1:4,1:4)))
     4.6250

EDU>> mean(mean(A(1:4,5:end)))
     3.7500

EDU>> mean(mean(A(5:end,1:4)))
     4.1875

EDU>> mean(mean(A(5:end,5:end)))
     3.9375

--Nasser

Subject: Averaging large Matrix using blocks

From: Jan Simon

Date: 2 Aug, 2013 12:39:10

Message: 7 of 8

Hi Matthew,

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

See: http://www.mathworks.com/matlabcentral/fileexchange/24812-blockmean

Beside the fast MEX, you find a Matlab implementation there also:

  % Get the size of the input as integer multiples of the block size:
  X = rand(400, 400);
  V = 4; % Number of columns
  W = 4; % Number of rows
  S = size(X);
  M = S(1) - mod(S(1), V);
  N = S(2) - mod(S(2), W);
  MV = M / V;
  NW = N / W;

  % Cut and reshape input such that the 1st and 3rd dimension have the lengths V
  % and W:
  XM = reshape(X(1:M, 1:N, :), V, MV, W, NW);

  % The calculations:
  Y = sum(sum(XM, 1), 3) ./ (V * W);

So the strategy is:
1. Crop the image to match the block size
2. Reshape the [MxN] matrix to [4, M/4, 4, N/4]
3. Calculate the sums over the 1st and 3rd dimension
4. Divide by the number of elements of the block

Kind regards, Jan

Subject: Averaging large Matrix using blocks

From: Matthew Balint

Date: 2 Aug, 2013 14:34:10

Message: 8 of 8

"Jan Simon" wrote in message <ktg99e$hcc$1@newscl01ah.mathworks.com>...



Thanks everyone for the replies! It has gotten me on my way! I appreciate it!

-Matt

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