From: Matt Fig on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <hvdu7i$for$1(a)fred.mathworks.com>...
> Just to be a stinker, I'll give a vectorized approach:
>
> % Data
> A = [1.3 3;2.2 2;3 2;3 3;4.5 1;5 1;6 0;-4 2];
>
>
> % Engine
> nz = A(:,2)~=0;
> idx = A(nz,1);
> CS = cumsum(A(nz,2));
> y = zeros(CS(end),1);
> y([1; CS(1:end-1)+1]) = 1;
> y = idx(cumsum(y))

Interesting us, I just downloaded RUDE, and you use almost the same exact engine inside. I guess this old problem has been around long enough...
From: dpb on
Matt Fig wrote:
> Just to be a stinker, I'll give a vectorized approach:
>
> % Data
> A = [1.3 3;2.2 2;3 2;3 3;4.5 1;5 1;6 0;-4 2];
>
> % Engine
> nz = A(:,2)~=0;
> idx = A(nz,1);
> CS = cumsum(A(nz,2));
> y = zeros(CS(end),1);
> y([1; CS(1:end-1)+1]) = 1;
> y = idx(cumsum(y))

Yeah, but I didn't want to think... :)

--
From: Bruno Luong on
What about a single-line code?

y = cumsum(accumarray(cumsum([1; A(1:end-1,2)]), diff([0; A(:,1)]), [sum(A(:,2)) 1]))

Bruno
From: dpb on
Bruno Luong wrote:
> What about a single-line code?
>
> y = cumsum(accumarray(cumsum([1; A(1:end-1,2)]), diff([0; A(:,1)]),
> [sum(A(:,2)) 1]))

there ya' go thinkin' again...and look what it got! :)

--
From: Matt Fig on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hve1ib$hps$1(a)fred.mathworks.com>...
> What about a single-line code?
>
> y = cumsum(accumarray(cumsum([1; A(1:end-1,2)]), diff([0; A(:,1)]), [sum(A(:,2)) 1]))
>
> Bruno



Single line indeed! I knew there was an accumarray solution in there somewhere, but I will cop to dpb's excuse!
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Smooth surface mesh/grid
Next: Memory Usage