From: koichan on
I have a vector of
x=[3 1 5 2 2 6]

I want to sum 3,1 and 5,2 and 2,6 seperately but i couldn't find the function to do.

Could anyone help me please?
From: Wayne King on
"koichan " <sadowgirlz(a)hotmail.com> wrote in message <hi3mn4$ods$1(a)fred.mathworks.com>...
> I have a vector of
> x=[3 1 5 2 2 6]
>
> I want to sum 3,1 and 5,2 and 2,6 seperately but i couldn't find the function to do.
>
> Could anyone help me please?
Hi Koichan, there are lots of ways to do that and without knowing more about how you want to apply this generally, it's hard for someone to suggest the most eloquent way.

One in this case:

sum(reshape(x,2,3))

wayne
From: ImageAnalyst on
You could use conv2, imfilter, and I think there's even one called
just plain "filter" that can all perform a sliding window average -
just pass in a kernel of all ones with a length of 2.
From: koichan on
"Wayne King" <wmkingty(a)gmail.com> wrote in message <hi4658$dae$1(a)fred.mathworks.com>...
> "koichan " <sadowgirlz(a)hotmail.com> wrote in message <hi3mn4$ods$1(a)fred.mathworks.com>...
> > I have a vector of
> > x=[3 1 5 2 2 6]
> >
> > I want to sum 3,1 and 5,2 and 2,6 seperately but i couldn't find the function to do.
> >
> > Could anyone help me please?
> Hi Koichan, there are lots of ways to do that and without knowing more about how you want to apply this generally, it's hard for someone to suggest the most eloquent way.
>
> One in this case:
>
> sum(reshape(x,2,3))
>
> wayne


Thank you so much
It works well with my model
thank you again
From: Matt J on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <e9d7c13d-71e3-4059-b121-756e69c610c5(a)z7g2000vbl.googlegroups.com>...
> You could use conv2, imfilter, and I think there's even one called
> just plain "filter" that can all perform a sliding window average -
> just pass in a kernel of all ones with a length of 2.
=====================


Just for fun, here's an interesting challenge that shows just how much of a pain in the neck it can be to generalize these solutions to sparse matrices.

Take the following data

[c,m]=computer;
x=sprand((m-3)/4+1,1,.001);

This data on my machine is only about 6.5 MB :


>> whos x
Name Size Bytes Class Attributes

x 536870912x1 6439724 double sparse


However, the reshaping technique won't work because MATLAB doesn't manage memory efficiently for sparse matrices with many, many columns (or at least not on my machine):

>> xbinned=sum(reshape(x,2,[]));
??? Error using ==> reshape
Out of memory. Type HELP MEMORY for your options.

Similarly, imfilter, conv2, etc... don't work because they aren't defined for sparse matrices.

The only 1-line method I could find was the following, which takes a really long time:


>> tic; xbinned=0.5*(x(1:2:end)+x(2:2:end)); toc
Elapsed time is 39.874281 seconds.


On the other hand, you can't really trust this method either because it uses linear indexing. But linear and logical indexing for sparse matrices have overflow bugs as shown for example in the following:

>> A=sparse(100000,100000,1),

A =

(100000,100000) 1

>> A(A>0)=2 %Wrong result due to indexing bug

A =

(65408,14101) 2
(100000,100000) 1