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:
Summing every x elements

Subject: Summing every x elements

From: James

Date: 8 Mar, 2006 12:19:24

Message: 1 of 20

Hello,

I was wondering if anyone know of a simple way to sum every x
elements down a column in a matrix. I cant seem to do this without a
loop.

For example x = 2;

M =
1 5 7
3 1 4
2 5 6
7 4 1

Ans =
4 6 11
9 9 7

help would be greatly appreciated.
thanks
James

Subject: Summing every x elements

From: Michael Wild

Date: 8 Mar, 2006 18:41:42

Message: 2 of 20

James wrote:
> Hello,
>
> I was wondering if anyone know of a simple way to sum every x
> elements down a column in a matrix. I cant seem to do this without a
> loop.
>
> For example x = 2;
>
> M =
> 1 5 7
> 3 1 4
> 2 5 6
> 7 4 1
>
> Ans =
> 4 6 11
> 9 9 7
>
> help would be greatly appreciated.
> thanks
> James


in this case:

M(1:2:end-1,:)+M(2:2:end,:)


can't think of a generic approach though.

michael

Subject: Summing every x elements

From: Jerome

Date: 8 Mar, 2006 12:59:03

Message: 3 of 20

Michael Wild wrote:

> can't think of a generic approach though.

Hi,

M =[1 5 7
3 1 4
2 5 6
7 4 1
5 3 6
8 6 9]

n=2

S=zeros(size(M,1)/n,size(M,2));
S(:)=sum(reshape(M,n,[]),1)

This is not a perfect solution
Jérôme

Subject: Summing every x elements

From: Michael Wild

Date: 8 Mar, 2006 18:51:36

Message: 4 of 20

Jérôme wrote:
> Michael Wild wrote:
>
>
>>can't think of a generic approach though.
>
>
> Hi,
>
> M =[1 5 7
> 3 1 4
> 2 5 6
> 7 4 1
> 5 3 6
> 8 6 9]
>
> n=2
>
> S=zeros(size(M,1)/n,size(M,2));
> S(:)=sum(reshape(M,n,[]),1)
>
> This is not a perfect solution
> Jérôme

good enough, but you think you don't need the ZEROS stuff as you don't
resize it.

make sure however, that size(M,1)/n is a positive integer.

michael

Subject: Summing every x elements

From: Jos

Date: 8 Mar, 2006 15:31:56

Message: 5 of 20

James wrote:
>
>
> Hello,
>
> I was wondering if anyone know of a simple way to sum every x
> elements down a column in a matrix. I cant seem to do this without
> a
> loop.
>
> For example x = 2;
>
> M =
> 1 5 7
> 3 1 4
> 2 5 6
> 7 4 1
>
> Ans =
> 4 6 11
> 9 9 7
>
> help would be greatly appreciated.
> thanks
> James

A generic approach:
% data
  M = ceil(10*rand(10,3)) ;
  n = 3 ; % sum every rows

%engine
  S = cumsum(M) ;
  S = S(n:n:end,:) ;
  S = S - [zeros(1,size(M,2)) ; S(1:end-1,:)] ;

%test
  isequal(S(1,:), sum(M(1:3,:)))
  isequal(S(2,:), sum(M(n+1:n+n,:)))

hth
Jos

Subject: Summing every x elements

From: PB

Date: 8 Mar, 2006 15:52:02

Message: 6 of 20

Jos wrote:
>
>
> James wrote:
>>
>>
>> Hello,
>>
>> I was wondering if anyone know of a simple way to sum every x
>> elements down a column in a matrix. I cant seem to do this
> without
>> a
>> loop.
>>
>> For example x = 2;
>>
>> M =
>> 1 5 7
>> 3 1 4
>> 2 5 6
>> 7 4 1
>>
>> Ans =
>> 4 6 11
>> 9 9 7
>>
>> help would be greatly appreciated.
>> thanks
>> James
>
> A generic approach:
> % data
> M = ceil(10*rand(10,3)) ;
> n = 3 ; % sum every rows
>
> %engine
> S = cumsum(M) ;
> S = S(n:n:end,:) ;
> S = S - [zeros(1,size(M,2)) ; S(1:end-1,:)] ;
>
> %test
> isequal(S(1,:), sum(M(1:3,:)))
> isequal(S(2,:), sum(M(n+1:n+n,:)))
>
> hth
> Jos

Another approach would be to use filter:

S2=filter(ones(1,n)',1,M);
S2=S2(n:n:end,:);

isequal(S,S2)

/PB

Subject: Summing every x elements

From: John D'Errico

Date: 8 Mar, 2006 20:48:01

Message: 7 of 20

In article <ef2b78e.-1@webx.raydaftYaTP>, James <None@thnks.com> wrote:

> Hello,
>
> I was wondering if anyone know of a simple way to sum every x
> elements down a column in a matrix. I cant seem to do this without a
> loop.
>
> For example x = 2;
>
> M =
> 1 5 7
> 3 1 4
> 2 5 6
> 7 4 1
>
> Ans =
> 4 6 11
> 9 9 7
>
> help would be greatly appreciated.
> thanks
> James

I don't know if its the best solution, but a pretty
one is certainly:

  [n,m] = size(M);

  ind = (1:n)';
  Msum = sparse(ceil(ind/x),ind,1)*M;

HTH,
John D'Errico


--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

Subject: Summing every x elements

From: Jos

Date: 8 Mar, 2006 17:25:46

Message: 8 of 20

John D'Errico wrote:
...

> I don't know if its the best solution, but a pretty
> one is certainly:
>
> [n,m] = size(M);
>
> ind = (1:n)';
> Msum = sparse(ceil(ind/x),ind,1)*M;
>
.

Subject: Summing every x elements

From: us

Date: 8 Mar, 2006 18:04:21

Message: 9 of 20

James:
<SNIP wants to stepwise <consolidate> his/her mat...

one of the many solutions...
...if(f) you're willing to download <john d'errico>'s great
child:

 <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8354&objectType=FILE>

     m=[
          1 5 7
          3 1 4
          2 5 6
          7 4 1
     ];
     s=2;
     sx=ceil((1:size(m,1))/s).';
     [r,r]=consolidator(sx,m,@sum);
     r

us

Subject: Summing every x elements

From: John D'Errico

Date: 9 Mar, 2006 10:05:24

Message: 10 of 20

In article <ef2b78e.6@webx.raydaftYaTP>, Jos <x@y.z> wrote:

> John D'Errico wrote:
> ...
>
> > I don't know if its the best solution, but a pretty
> > one is certainly:
> >
> > [n,m] = size(M);
> >
> > ind = (1:n)';
> > Msum = sparse(ceil(ind/x),ind,1)*M;
> >
> ..
>
> John,
> Beautiful!
> However, I think you really meant
>
> M = ...
> x = 3 ;
> [n,m] = size(M) ;
> S2 = full(sparse(repmat(ceil([1:n]/x),1,m),repmat(1:m,n,1),M)) ;
>
> Jos

Hi Jos,

Nope. I meant what I said. Try mine out. I'd considered
using sparse as you did, an entirely valid solution in
itself.

Your solution uses sparse as a summing tool, mine uses
sparse only to create a sparse block diagonal matrix,
then the matrix multiply does the sum.

By the way, another solution would use the chunkify code
recently posted on the FEX:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10004&objectType=file

Something like this:

 n = size(M,1);
 c = chunkify(ones(1,n),x);
 Msum = blkdiag(c{:})*M;

John


--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

Subject: Summing every x elements

From: M. Tetouan

Date: 9 Mar, 2006 09:00:51

Message: 11 of 20

James wrote:
>
>
> Hello,
>
> I was wondering if anyone know of a simple way to sum every x
> elements down a column in a matrix. I cant seem to do this without
> a
> loop.
>
> For example x = 2;
>
> M =
> 1 5 7
> 3 1 4
> 2 5 6
> 7 4 1
>
> Ans =
> 4 6 11
> 9 9 7
>
> help would be greatly appreciated.
> thanks
> James

Another solution:

>> x = 2; d = size(m,2);
>> result = blkproc(m,[x d],@sum)

Subject: Summing every x elements

From: Jerome

Date: 9 Mar, 2006 12:51:28

Message: 12 of 20

M. Tetouan wrote:

>>> result = blkproc(m,[x d],@sum)

Note that BLKPROC.M is part of the IP toolbox.

Jérôme

Subject: Summing every x elements

From: Jos

Date: 9 Mar, 2006 14:27:56

Message: 13 of 20

John D'Errico wrote:
>
> Nope. I meant what I said. Try mine out. I'd considered
> using sparse as you did, an entirely valid solution in
> itself.

I stand corrected.

Subject: Summing every x elements

From: Pat Callan

Date: 6 Apr, 2006 10:38:29

Message: 14 of 20

John D'Errico wrote:
>
>
> In article <ef2b78e.-1@webx.raydaftYaTP>, James
<None@thnks.com>
> wrote:
>
>> Hello,
>>
>> I was wondering if anyone know of a simple way to sum every x
>> elements down a column in a matrix. I cant seem to do this
> without a
>> loop.
>>
>> For example x = 2;
>>
>> M =
>> 1 5 7
>> 3 1 4
>> 2 5 6
>> 7 4 1
>>
>> Ans =
>> 4 6 11
>> 9 9 7
>>
>> help would be greatly appreciated.
>> thanks
>> James
>
> I don't know if its the best solution, but a pretty
> one is certainly:
>
> [n,m] = size(M);
>
> ind = (1:n)';
> Msum = sparse(ceil(ind/x),ind,1)*M;
>
> HTH,
> John D'Errico
>
>
> --
> The best material model of a cat is another, or preferably the
> same, cat.
> A. Rosenblueth, Philosophy of Science, 1945
>
> Those who can't laugh at themselves leave the job to others.
> Anonymous
>

John,
Might there be a row-segment sum and/or an "ignore NaN" correlary to
this approach?

Pat

Subject: Summing every x elements

From: Steve Amphlett

Date: 6 Apr, 2006 11:22:46

Message: 15 of 20

James wrote:
>
>
> Hello,
>
> I was wondering if anyone know of a simple way to sum every x
> elements down a column in a matrix. I cant seem to do this without
> a
> loop.
>
> For example x = 2;
>
> M =
> 1 5 7
> 3 1 4
> 2 5 6
> 7 4 1
>
> Ans =
> 4 6 11
> 9 9 7
>
> help would be greatly appreciated.
> thanks
> James

Very, very late in the thread I know, but...

reshape(sum(reshape(M,x,[])),size(M,1)/x,[])

Subject: Summing every x elements

From: John D'Errico

Date: 6 Apr, 2006 15:13:40

Message: 16 of 20

In article <ef2b78e.12@webx.raydaftYaTP>, "Pat Callan" <callan_p@sonalysts.com> wrote:

> John D'Errico wrote:
> >
> >
> > In article <ef2b78e.-1@webx.raydaftYaTP>, James
> <None@thnks.com>
> > wrote:
> >
> >> Hello,
> >>
> >> I was wondering if anyone know of a simple way to sum every x
> >> elements down a column in a matrix. I cant seem to do this
> > without a
> >> loop.
> >>
> >> For example x = 2;
> >>
> >> M =
> >> 1 5 7
> >> 3 1 4
> >> 2 5 6
> >> 7 4 1
> >>
> >> Ans =
> >> 4 6 11
> >> 9 9 7
> >>
> >> help would be greatly appreciated.
> >> thanks
> >> James
> >
> > I don't know if its the best solution, but a pretty
> > one is certainly:
> >
> > [n,m] = size(M);
> >
> > ind = (1:n)';
> > Msum = sparse(ceil(ind/x),ind,1)*M;
> >
> > HTH,
> > John D'Errico
> >
> >
> > --
> > The best material model of a cat is another, or preferably the
> > same, cat.
> > A. Rosenblueth, Philosophy of Science, 1945
> >
> > Those who can't laugh at themselves leave the job to others.
> > Anonymous
> >
>
> John,
> Might there be a row-segment sum and/or an "ignore NaN" correlary to
> this approach?
>
> Pat

Row sums are easy enough.

[n,m] = size(M);

ind = (1:m)';
Msum = M*sparse(ind,ceil(ind/x),1);


Ignore NaNs? This I can't do, because the matrix
multiply operates on all columns or rows essentially
in parallel. So a single NaN element cannot be ignored.

John


--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

Subject: Summing every x elements

From: Jos

Date: 6 Apr, 2006 15:47:09

Message: 17 of 20

John D'Errico wrote:

> Ignore NaNs? This I can't do, because the matrix
> multiply operates on all columns or rows essentially
> in parallel. So a single NaN element cannot be ignored.
>
> John
>

why not simply set the NaNs to zero?

Jos

Subject: Summing every x elements

From: John D'Errico

Date: 6 Apr, 2006 21:48:01

Message: 18 of 20

In article <ef2b78e.15@webx.raydaftYaTP>, Jos <x@y.z> wrote:

> John D'Errico wrote:
>
> > Ignore NaNs? This I can't do, because the matrix
> > multiply operates on all columns or rows essentially
> > in parallel. So a single NaN element cannot be ignored.
> >
> > John
> >
>
> why not simply set the NaNs to zero?
>
> Jos

Doh!

Yes, do. You could even use a variant of this to
compute the mean.

John


--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

Subject: Summing every x elements

From: Pat Callan

Date: 7 Apr, 2006 08:54:33

Message: 19 of 20

John D'Errico wrote:
>
>
> In article <ef2b78e.15@webx.raydaftYaTP>, Jos <x@y.z>
wrote:
>
>> John D'Errico wrote:
>>
>> > Ignore NaNs? This I can't do, because the matrix
>> > multiply operates on all columns or rows essentially
>> > in parallel. So a single NaN element cannot be ignored.
>> >
>> > John
>> >
>>
>> why not simply set the NaNs to zero?
>>
>> Jos
>
> Doh!
>
> Yes, do. You could even use a variant of this to
> compute the mean.
>
> John
>
>
> --
> The best material model of a cat is another, or preferably the
> same, cat.
> A. Rosenblueth, Philosophy of Science, 1945
>
> Those who can't laugh at themselves leave the job to others.
> Anonymous
>
John and Jos,
Thanks - will try both approaches. Good info.

Pat

Subject: Summing every x elements

From: Markella

Date: 13 Nov, 2013 10:20:25

Message: 20 of 20

Jos <x@y.z> wrote in message <ef2b78e.3@webx.raydaftYaTP>...
> James wrote:
> >
> >
> > Hello,
> >
> > I was wondering if anyone know of a simple way to sum every x
> > elements down a column in a matrix. I cant seem to do this without
> > a
> > loop.
> >
> > For example x = 2;
> >
> > M =
> > 1 5 7
> > 3 1 4
> > 2 5 6
> > 7 4 1
> >
> > Ans =
> > 4 6 11
> > 9 9 7
> >
> > help would be greatly appreciated.
> > thanks
> > James
>
> A generic approach:
> % data
> M = ceil(10*rand(10,3)) ;
> n = 3 ; % sum every rows
>
> %engine
> S = cumsum(M) ;
> S = S(n:n:end,:) ;
> S = S - [zeros(1,size(M,2)) ; S(1:end-1,:)] ;
>
> %test
> isequal(S(1,:), sum(M(1:3,:)))
> isequal(S(2,:), sum(M(n+1:n+n,:)))
>
> hth
> Jos

Really good approach Jos, thanks

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