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:
faster method to re-grid data

Subject: faster method to re-grid data

From: edward kabanyas

Date: 21 Sep, 2011 05:05:29

Message: 1 of 6

Hi all,

I plan to re-grid my data. The data example:

10 10 10 10 12 12 12 12
10 10 10 10 12 12 12 12
10 10 10 10 12 12 12 12
10 10 10 10 12 12 12 12
11 11 11 11 13 13 13 13
11 11 11 11 13 13 13 13
11 11 11 11 13 13 13 13
11 11 11 11 13 13 13 13

assuming the data name is data. I want to calculate the average of each 4x 4, from the example, the output must be:

10 12
11 13

I used the following simple code to do it:

index =0;
index1 =0;
for ii =1:4:8
    index = index+1;
    for jj=1:4:8
        index1 = index1+1;
        datax=data(ii:ii+3,jj:jj+3);
        avg1(index, index1)=mean(mean(datax,1),2);
    end
    index1 =0;
end
index =0;

It works, but, because of looping process the code is very slow, particularly if I use for a big data file. Do you have any suggestion to make it faster ? may be reduce the loop.

Thanks for help

Edward

Subject: faster method to re-grid data

From: Bruno Luong

Date: 21 Sep, 2011 06:23:08

Message: 2 of 6

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j5brap$iiq$1@newscl01ah.mathworks.com>...
> Hi all,
>
> I plan to re-grid my data. The data example:
>
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
>
> assuming the data name is data. I want to calculate the average of each 4x 4, from the example, the output must be:
>
> 10 12
> 11 13
>
> I used the following simple code to do it:
>
> index =0;
> index1 =0;
> for ii =1:4:8
> index = index+1;
> for jj=1:4:8
> index1 = index1+1;
> datax=data(ii:ii+3,jj:jj+3);
> avg1(index, index1)=mean(mean(datax,1),2);
> end
> index1 =0;
> end
> index =0;
>
> It works, but, because of looping process the code is very slow, particularly if I use for a big data file. Do you have any suggestion to make it faster ? may be reduce the loop.

A=[10 10 10 10 12 12 12 12
    10 10 10 10 12 12 12 12
    10 10 10 10 12 12 12 12
    10 10 10 10 12 12 12 12
    11 11 11 11 13 13 13 13
    11 11 11 11 13 13 13 13
    11 11 11 11 13 13 13 13
    11 11 11 11 13 13 13 13]

%%%%%%%%%%%%%%%%%%%%
% Engine 1
B = A;
blk = 4;
[m n] = size(B);

% Pad 0 if size is not divided buy blk
p = ceil(m/blk)*blk;
q = ceil(n/blk)*blk;
if m<p
    B(p,end)=0;
end
if n<q
    B(end,q)=0;
end

C = zeros(size(B));
C(1:m,1:n) = 1;

B = reshape(B,blk,p/blk,blk,q/blk);
C = reshape(C,blk,p/blk,blk,q/blk);
AA = squeeze(sum(sum(B,1),3) ./ sum(sum(C,1),3))

%%%%%%%%%%%%%%%%%%%%
% Engine 2
blk = 4;
[m n] = size(A);
[I J] = ndgrid(floor((0:m-1)/blk)+1,floor((0:n-1)/blk)+1);
AA = accumarray([I(:) J(:)], A(:))./ accumarray([I(:) J(:)], 1)

% Bruno

Subject: faster method to re-grid data

From: edward kabanyas

Date: 21 Sep, 2011 07:04:25

Message: 3 of 6

Hi Bruno;

Thanks for very nice code, it works perfectly and very fast in comparison with my code.

Actually, there two things that I want to do, first calculate the mean and the second one is to calculate the percentage of occurrence. For example the occurrence of the data <= 11 in 4 x 4 array, I calculate as:

 Tot_data(index, index1)=(sum(nansum(datax >=11,1),2)/16)*100;

Could you suggest any solution for this ? If I use the above calculation, I must put in a loop as before (after > > avg1(index, index1)=mean(mean(datax,1),2)). If you could help, I really appreciate it. Again, thanks so much.

Best regards,
Edward



"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j5bvsc$4ag$1@newscl01ah.mathworks.com>...
> "edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j5brap$iiq$1@newscl01ah.mathworks.com>...
> > Hi all,
> >
> > I plan to re-grid my data. The data example:
> >
> > 10 10 10 10 12 12 12 12
> > 10 10 10 10 12 12 12 12
> > 10 10 10 10 12 12 12 12
> > 10 10 10 10 12 12 12 12
> > 11 11 11 11 13 13 13 13
> > 11 11 11 11 13 13 13 13
> > 11 11 11 11 13 13 13 13
> > 11 11 11 11 13 13 13 13
> >
> > assuming the data name is data. I want to calculate the average of each 4x 4, from the example, the output must be:
> >
> > 10 12
> > 11 13
> >
> > I used the following simple code to do it:
> >
> > index =0;
> > index1 =0;
> > for ii =1:4:8
> > index = index+1;
> > for jj=1:4:8
> > index1 = index1+1;
> > datax=data(ii:ii+3,jj:jj+3);
> > avg1(index, index1)=mean(mean(datax,1),2);
> > end
> > index1 =0;
> > end
> > index =0;
> >
> > It works, but, because of looping process the code is very slow, particularly if I use for a big data file. Do you have any suggestion to make it faster ? may be reduce the loop.
>
> A=[10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13]
>
> %%%%%%%%%%%%%%%%%%%%
> % Engine 1
> B = A;
> blk = 4;
> [m n] = size(B);
>
> % Pad 0 if size is not divided buy blk
> p = ceil(m/blk)*blk;
> q = ceil(n/blk)*blk;
> if m<p
> B(p,end)=0;
> end
> if n<q
> B(end,q)=0;
> end
>
> C = zeros(size(B));
> C(1:m,1:n) = 1;
>
> B = reshape(B,blk,p/blk,blk,q/blk);
> C = reshape(C,blk,p/blk,blk,q/blk);
> AA = squeeze(sum(sum(B,1),3) ./ sum(sum(C,1),3))
>
> %%%%%%%%%%%%%%%%%%%%
> % Engine 2
> blk = 4;
> [m n] = size(A);
> [I J] = ndgrid(floor((0:m-1)/blk)+1,floor((0:n-1)/blk)+1);
> AA = accumarray([I(:) J(:)], A(:))./ accumarray([I(:) J(:)], 1)
>
> % Bruno

Subject: faster method to re-grid data

From: Bruno Luong

Date: 21 Sep, 2011 08:54:30

Message: 4 of 6

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j5c29p$c9b$1@newscl01ah.mathworks.com>...
> Hi Bruno;
>
> Thanks for very nice code, it works perfectly and very fast in comparison with my code.
>
> Actually, there two things that I want to do, first calculate the mean and the second one is to calculate the percentage of occurrence. For example the occurrence of the data <= 11 in 4 x 4 array, I calculate as:
>

Simply replace "A" with "A<=11"

A = A<=11

then do the rest of the calculation as with mean.

Bruno

Subject: faster method to re-grid data

From: Doug Schwarz

Date: 21 Sep, 2011 11:50:41

Message: 5 of 6

In article <j5brap$iiq$1@newscl01ah.mathworks.com>,
 "edward kabanyas" <djuky_hmi@yahoo.com> wrote:

> Hi all,
>
> I plan to re-grid my data. The data example:
>
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 10 10 10 10 12 12 12 12
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
> 11 11 11 11 13 13 13 13
>
> assuming the data name is data. I want to calculate the average of each 4x 4,
> from the example, the output must be:
>
> 10 12
> 11 13
>
> I used the following simple code to do it:
>
> index =0;
> index1 =0;
> for ii =1:4:8
> index = index+1;
> for jj=1:4:8
> index1 = index1+1;
> datax=data(ii:ii+3,jj:jj+3);
> avg1(index, index1)=mean(mean(datax,1),2);
> end
> index1 =0;
> end
> index =0;
>
> It works, but, because of looping process the code is very slow, particularly
> if I use for a big data file. Do you have any suggestion to make it faster ?
> may be reduce the loop.
>
> Thanks for help
>
> Edward


A = conv2(data,ones(4,4)/16);
B = A(4:4:end,4:4:end);

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: faster method to re-grid data

From: Matt J

Date: 21 Sep, 2011 13:12:29

Message: 6 of 6

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <j5brap$iiq$1@newscl01ah.mathworks.com>...
>
> assuming the data name is data. I want to calculate the average of each 4x 4, from the example, the output must be:
>
> 10 12
> 11 13
=================

You're basically downsampling by pixel averaging. The following is the tool I use.


function M=downsamp2d(M,bindims)
%DOWNSAMP2D - simple tool for 2D downsampling
%
% M=downsamp2d(M,bindims)
%
%in:
%
% M: a matrix
% bindims: a vector [p,q] specifying pxq downsampling
%
%out:
%
% M: the downsized matrix

p=bindims(1); q=bindims(2);
[m,n]=size(M); %M is the original matrix

M=sum( reshape(M,p,[]) ,1 );
M=reshape(M,m/p,[]).'; %Note transpose

M=sum( reshape(M,q,[]) ,1);
M=reshape(M,n/q,[]).'; %Note transpose

M=M/(p*q);

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