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:
accumarray with cumsum function

Subject: accumarray with cumsum function

From: Rawley

Date: 26 Jan, 2011 19:00:08

Message: 1 of 2

I'm trying to use cumsum across bins of data. If I have an indicator variable in the first column and want to use cumsum in the second column as follows:

X =

     1 5
     1 3
     1 10
     3 4
     3 15

I would like the output to be:

Y =

     1 5
     1 8
     1 18
     3 4
     3 19

I've tried:

[b, I, bin] = unique(X(:,1));
Y = accumarray(bin,X(:,2),[5 1],@cumsum);

I receive the error message: "??? Error using ==> accumarray
The function 'cumsum' returned a non-scalar value."

Does anyone know how to troubleshoot this? Thanks in advance.

Subject: accumarray with cumsum function

From: Sean de

Date: 26 Jan, 2011 19:57:03

Message: 2 of 2

"Rawley " <ballheim@gmail.com> wrote in message <ihpqvo$fp9$1@fred.mathworks.com>...
> I'm trying to use cumsum across bins of data. If I have an indicator variable in the first column and want to use cumsum in the second column as follows:
>
> X =
>
> 1 5
> 1 3
> 1 10
> 3 4
> 3 15
>
> I would like the output to be:
>
> Y =
>
> 1 5
> 1 8
> 1 18
> 3 4
> 3 19


Y(:,1) = X(:,1);
Y(:,2) = cell2mat(accumarray(X(:,1),X(:,2),[],@(x){cumsum(x)}))

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