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:
blockwise mean of large matrix

Subject: blockwise mean of large matrix

From: soms.sharma@gmail.com

Date: 30 Dec, 2008 20:07:17

Message: 1 of 7

Greetings!

I have a 100*100 sized matrix of a variable. Is there some quick ways/
syntax to get mean of the 10*10 blocks of the original matrix starting
from the left upper edge?
Thanks
Soms

Subject: blockwise mean of large matrix

From: Matt

Date: 30 Dec, 2008 20:15:04

Message: 2 of 7

soms.sharma@gmail.com wrote in message <82af7ae9-44fd-4745-b85a-ef77ed0693b7@i24g2000prf.googlegroups.com>...
> Greetings!
>
> I have a 100*100 sized matrix of a variable. Is there some quick ways/
> syntax to get mean of the 10*10 blocks of the original matrix starting
> from the left upper edge?
> Thanks
> Soms

Call the matrix X.

partition=10*ones(1,10);
Xcell=mat2cell(X,partition,partition);
XX=horzcat(Xcell{:});
Means=mean( reshape(XX,100,[]) ,1);

Subject: blockwise mean of large matrix

From: ImageAnalyst

Date: 30 Dec, 2008 20:20:33

Message: 3 of 7

On Dec 30, 3:07=A0pm, soms.sha...@gmail.com wrote:
> Greetings!
>
> I have a 100*100 sized matrix of a variable. Is there some quick ways/
> syntax to get mean of the 10*10 blocks of the original matrix starting
> from the left upper edge?
> Thanks
> Soms

---------------------------------------------------------------------------=
----------------
Soms:
Yes, you can use the conv2() function with a uniform kernel (box
filter) to do that. This will give every pixel in the output image
that is the average of a 10 by 10 window. If you don't want the
windows to overlap, then you can still use the conv2() function but
just take every 10th element of the output. If you have the image
processing toolbox, you can use also use blkproc() to do fancier
things than averaging.
Regards,
ImageAnalyst

Subject: blockwise mean of large matrix

From: soms.sharma@gmail.com

Date: 30 Dec, 2008 21:32:39

Message: 4 of 7

On Dec 30, 12:20=A0pm, ImageAnalyst <imageanal...@mailinator.com> wrote:
> On Dec 30, 3:07=A0pm, soms.sha...@gmail.com wrote:
>
> > Greetings!
>
> > I have a 100*100 sized matrix of a variable. Is there some quick ways/
> > syntax to get mean of the 10*10 blocks of the original matrix starting
> > from the left upper edge?
> > Thanks
> > Soms
>
> -------------------------------------------------------------------------=
------------------
> Soms:
> Yes, you can use the conv2() function with a uniform kernel (box
> filter) to do that. =A0This will give every pixel in the output image
> that is the average of a 10 by 10 window. =A0If you don't want the
> windows to overlap, then you can still use the conv2() function but
> just take every 10th element of the output. =A0

Thanks! would you pl. show me that with a few lines of script to
accomplish the goal?


If you have the image
> processing toolbox, you can use also use blkproc() to do fancier
> things than averaging.
> Regards,
> ImageAnalyst

Subject: blockwise mean of large matrix

From: Image Analyst

Date: 30 Dec, 2008 22:10:18

Message: 5 of 7

soms.sharma@gmail.com wrote in message <c8693148-29f2-44f3-88fe-8662a4d03aaa@n33g2000pri.googlegroups.com>...
> On Dec 30, 12:20=A0pm, ImageAnalyst <imageanal...@mailinator.com> wrote:
> > On Dec 30, 3:07=A0pm, soms.sha...@gmail.com wrote:
> >
> > > Greetings!
> >
> > > I have a 100*100 sized matrix of a variable. Is there some quick ways/
> > > syntax to get mean of the 10*10 blocks of the original matrix starting
> > > from the left upper edge?
> > > Thanks
> > > Soms
> >
> > -------------------------------------------------------------------------=
> ------------------
> > Soms:
> > Yes, you can use the conv2() function with a uniform kernel (box
> > filter) to do that. =A0This will give every pixel in the output image
> > that is the average of a 10 by 10 window. =A0If you don't want the
> > windows to overlap, then you can still use the conv2() function but
> > just take every 10th element of the output. =A0
>
> Thanks! would you pl. show me that with a few lines of script to
> accomplish the goal?
>
>
> If you have the image
> > processing toolbox, you can use also use blkproc() to do fancier
> > things than averaging.
> > Regards,
> > ImageAnalyst
---------------------------------------------------------------------------------
Soms:
Here's some demo code for you.
By the way, 100 by 100 is not a huge matrix. In fact, it's rather tiny. That would be only 80 kilobytes. Now if you were dealing with a matirx that was ten thousand times that big, then that would be in the large catagory. My matrices would be 2 - 18 gigabytes (but MATLAB can't handle the whole thing in memory at one time so we have to play tricks to deal with them). Some oil and gas researchers are dealing with seismic datasets that are 10 terabytes in size. Now THAT'S a big image!

clc;
close all;
% Read in an image.
originalImage = imread('cameraman.tif');
subplot(1,4,1);
imshow(originalImage);
% Extract a small 100 by 100 chunk of the larger image.
x1 = 75;
x2 = x1 + 99;
y1 = 30;
y2 = y1 + 99;
hundredImage = double(originalImage(y1:y2, x1:x2));
% Display the smaller image.
subplot(1,4,2);
imshow(hundredImage, []);
% Make a box filter to average all the values within a 10 by 10 window.
boxFilter = ones(10,10);
% Normalize so that the intensity won't change.
% Otherwise it will just add up 100 values in the window. Need to divide by 100.
boxFilter = boxFilter ./ 100;
averagedImage = conv2(hundredImage, boxFilter);
% Display the smaller image.
subplot(1,4,3);
imshow(averagedImage, []);
% Subsample it by 10 in case he wanted non-overlapping windows.
% Start at 6 to avoid edge effects caused by averaging in zeros
% for those pixels in the window outside the image when you're near the edge.
subsampledImage = averagedImage(6:10:(end-6), 6:10:(end-6));
% Display the subsampled image.
subplot(1,4,4);
imshow(subsampledImage, []);
subsampledImage % Print to command window.


Regards,
ImageAnalyst

Subject: blockwise mean of large matrix

From: Roger Stafford

Date: 30 Dec, 2008 23:01:05

Message: 6 of 7

soms.sharma@gmail.com wrote in message <82af7ae9-44fd-4745-b85a-ef77ed0693b7@i24g2000prf.googlegroups.com>...
> Greetings!
>
> I have a 100*100 sized matrix of a variable. Is there some quick ways/
> syntax to get mean of the 10*10 blocks of the original matrix starting
> from the left upper edge?
> Thanks
> Soms

  You could also do this. Let A be an original array with n x n blocks, with each block of m x m size.

 m = 10; n = 10; % n x n blocks, each of m x m size
 [I,J,K,L] = ndgrid(1:m,0:m-1,0:n-1,0:n-1);
 M = reshape(mean(reshape(A(I+m*n*J+m*K+m^2*n*L),m^2,n^2),1),n,n);

M will be the desired array of mean values for the blocks

Roger Stafford

Subject: blockwise mean of large matrix

From: Roger Stafford

Date: 31 Dec, 2008 02:09:02

Message: 7 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message
> You could also do this. Let A be an original array with n x n blocks, with each block of m x m size.
>
> m = 10; n = 10; % n x n blocks, each of m x m size
> [I,J,K,L] = ndgrid(1:m,0:m-1,0:n-1,0:n-1);
> M = reshape(mean(reshape(A(I+m*n*J+m*K+m^2*n*L),m^2,n^2),1),n,n);
>
> M will be the desired array of mean values for the blocks
>
> Roger Stafford

  I should have generalized that method a little more. If array A consists of p x q blocks, each of size m x n, (and therefore A is of size m*p x n*q,) then the array M gives the corresponding p x q array of the blocks' mean values.

 [I,J,K,L] = ndgrid(1:m,0:n-1,0:p-1,0:q-1);
 M = reshape(mean(reshape(A(I+m*p*J+m*K+m*p*n*L),m*n,p*q),1),p,q);

Roger Stafford

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