Thread Subject: Data Bin

Subject: Data Bin

From: AP Pal

Date: 4 Oct, 2009 15:43:01

Message: 1 of 8

I am new to matlab and am stuck with what seems simple task
i have a column of data 1000 pts long. i need to average in groups of 10 so i need to generate a new matrix which has 100pts.

i was trying to write a code with a mini file and keep making mistakes

p=X
n=1;
i=1;
m=length(X);

avg_pts= 2;
z= length(X)/avg_pts
% q=0;
p(1,1)=X(1,1);

  for i=(1:1:z);
     
       for q=(1:1:avg_pts);
           if n+q<=m
       p(q,1)=p(q,1)+X(n+q,1);
           else
           end
       end
       y(i,1)=p(q,1)/avg_pts;
      n=n+avg_pts;
      
  end
 

Any help will be greatly appreciated
thanks

Subject: Data Bin

From: Andrew Stevens

Date: 4 Oct, 2009 17:05:03

Message: 2 of 8

"AP Pal" <avispal@gmail.com> wrote in message <haafq5$mvs$1@fred.mathworks.com>...
> I am new to matlab and am stuck with what seems simple task
> i have a column of data 1000 pts long. i need to average in groups of 10 so i need to generate a new matrix which has 100pts.
>
> i was trying to write a code with a mini file and keep making mistakes
>
> p=X
> n=1;
> i=1;
> m=length(X);
>
> avg_pts= 2;
> z= length(X)/avg_pts
> % q=0;
> p(1,1)=X(1,1);
>
> for i=(1:1:z);
>
> for q=(1:1:avg_pts);
> if n+q<=m
> p(q,1)=p(q,1)+X(n+q,1);
> else
> end
> end
> y(i,1)=p(q,1)/avg_pts;
> n=n+avg_pts;
>
> end
>
>
> Any help will be greatly appreciated
> thanks

Hi,

You many want to try something like:

%generate a dataset
y=rand(1000,1);

%average in 10 point bins
ym=mean(reshape(y,10,100));

-Andrew

Subject: Data Bin

From: AP Pal

Date: 4 Oct, 2009 19:15:03

Message: 3 of 8


Thanks a lot Andrew, it really saved me so much headache.

"Andrew Stevens" <astevens@JUNKusgs.gov> wrote in message <haakjv$6r1$1@fred.mathworks.com>...
> "AP Pal" <avispal@gmail.com> wrote in message <haafq5$mvs$1@fred.mathworks.com>...
> > I am new to matlab and am stuck with what seems simple task
> > i have a column of data 1000 pts long. i need to average in groups of 10 so i need to generate a new matrix which has 100pts.
> >
> > i was trying to write a code with a mini file and keep making mistakes
> >
> > p=X
> > n=1;
> > i=1;
> > m=length(X);
> >
> > avg_pts= 2;
> > z= length(X)/avg_pts
> > % q=0;
> > p(1,1)=X(1,1);
> >
> > for i=(1:1:z);
> >
> > for q=(1:1:avg_pts);
> > if n+q<=m
> > p(q,1)=p(q,1)+X(n+q,1);
> > else
> > end
> > end
> > y(i,1)=p(q,1)/avg_pts;
> > n=n+avg_pts;
> >
> > end
> >
> >
> > Any help will be greatly appreciated
> > thanks
>
> Hi,
>
> You many want to try something like:
>
> %generate a dataset
> y=rand(1000,1);
>
> %average in 10 point bins
> ym=mean(reshape(y,10,100));
>
> -Andrew

Subject: Data Bin

From: AP Pal

Date: 5 Oct, 2009 16:49:03

Message: 4 of 8

Any fast way to reshape the whole matrix?
i mean converting from 100X 50
to 10X 50

by averaging the 10 rows at a time to bin

the reshape funtion worked for n X 1 matrix only.

"AP Pal" <avispal@gmail.com> wrote in message <haafq5$mvs$1@fred.mathworks.com>...
> I am new to matlab and am stuck with what seems simple task
> i have a column of data 1000 pts long. i need to average in groups of 10 so i need to generate a new matrix which has 100pts.
>
> i was trying to write a code with a mini file and keep making mistakes
>
> p=X
> n=1;
> i=1;
> m=length(X);
>
> avg_pts= 2;
> z= length(X)/avg_pts
> % q=0;
> p(1,1)=X(1,1);
>
> for i=(1:1:z);
>
> for q=(1:1:avg_pts);
> if n+q<=m
> p(q,1)=p(q,1)+X(n+q,1);
> else
> end
> end
> y(i,1)=p(q,1)/avg_pts;
> n=n+avg_pts;
>
> end
>
>
> Any help will be greatly appreciated
> thanks

Subject: Data Bin

From: AP Pal

Date: 5 Oct, 2009 16:49:03

Message: 5 of 8

Any fast way to reshape the whole matrix?
i mean converting from 100X 50
to 10X 50

by averaging the 10 rows at a time to bin

the reshape funtion worked for n X 1 matrix only.

"AP Pal" <avispal@gmail.com> wrote in message <haafq5$mvs$1@fred.mathworks.com>...
> I am new to matlab and am stuck with what seems simple task
> i have a column of data 1000 pts long. i need to average in groups of 10 so i need to generate a new matrix which has 100pts.
>
> i was trying to write a code with a mini file and keep making mistakes
>
> p=X
> n=1;
> i=1;
> m=length(X);
>
> avg_pts= 2;
> z= length(X)/avg_pts
> % q=0;
> p(1,1)=X(1,1);
>
> for i=(1:1:z);
>
> for q=(1:1:avg_pts);
> if n+q<=m
> p(q,1)=p(q,1)+X(n+q,1);
> else
> end
> end
> y(i,1)=p(q,1)/avg_pts;
> n=n+avg_pts;
>
> end
>
>
> Any help will be greatly appreciated
> thanks

Subject: Data Bin

From: Jan Simon

Date: 5 Oct, 2009 20:01:20

Message: 6 of 8

Dear AP Pal!

> Any fast way to reshape the whole matrix?
> i mean converting from 100X 50
> to 10X 50
>
> by averaging the 10 rows at a time to bin
>
> the reshape funtion worked for n X 1 matrix only.

No, RESHAPE works really fast for any array, not just for [n x 1] matrices!

The mentioned solution is efficient. A little bit faster, because it avoids the overhead of calling the function MEAN:
  X = rand(100, 50);
  Y = sum(reshape(X, 10, 10, 50), 1) / 10;

Kind regards, Jan

Subject: Data Bin

From: AP Pal

Date: 5 Oct, 2009 22:12:03

Message: 7 of 8

Thanks a lot Jan .. you rock!!
now only if some one answers in my other thread to add listbox i will be set( for the time being) :P


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hadjaf$pn1$1@fred.mathworks.com>...
> Dear AP Pal!
>
> > Any fast way to reshape the whole matrix?
> > i mean converting from 100X 50
> > to 10X 50
> >
> > by averaging the 10 rows at a time to bin
> >
> > the reshape funtion worked for n X 1 matrix only.
>
> No, RESHAPE works really fast for any array, not just for [n x 1] matrices!
>
> The mentioned solution is efficient. A little bit faster, because it avoids the overhead of calling the function MEAN:
> X = rand(100, 50);
> Y = sum(reshape(X, 10, 10, 50), 1) / 10;
>
> Kind regards, Jan

Subject: Data Bin

From: Jos

Date: 6 Oct, 2009 09:05:06

Message: 8 of 8

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hadjaf$pn1$1@fred.mathworks.com>...
> Dear AP Pal!
>
> > Any fast way to reshape the whole matrix?
> > i mean converting from 100X 50
> > to 10X 50
> >
> > by averaging the 10 rows at a time to bin
> >
> > the reshape funtion worked for n X 1 matrix only.
>
> No, RESHAPE works really fast for any array, not just for [n x 1] matrices!
>
> The mentioned solution is efficient. A little bit faster, because it avoids the overhead of calling the function MEAN:
> X = rand(100, 50);
> Y = sum(reshape(X, 10, 10, 50), 1) / 10;
>
> Kind regards, Jan

If the number of elements is not a multiple of the number you want to average over, you can use ACCUMARRAY

A = 1:8
n = 3 ;

% M = sum(reshape(A,n,[]))/10 % fails !!
idx = 1:numel(A)
groupnr = ceil(idx / n)
M = accumarray(groupnr(:),A(:),[],@mean)
% or in 1 step,
% M = accumarray(ceil((1:numel(A))/n).',A.',[],@mean)

Jos

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
mean Jos 6 Oct, 2009 05:09:06
accumarray Jos 6 Oct, 2009 05:09:06
reshape Jos 6 Oct, 2009 05:09:06
reshape Andrew Stevens 4 Oct, 2009 13:09:08
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com