From: Bruno Luong on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <hve28s$33d$1(a)fred.mathworks.com>...

> Single line indeed! I knew there was an accumarray solution in there somewhere, but I will cop to dpb's excuse!

Oh common, you guys are hopeless about admiring the poesy of accumarray. I'm going to sleep.

Bruno
From: Matt Fig on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hve2gc$ht6$1(a)fred.mathworks.com>...
> "Matt Fig" <spamanon(a)yahoo.com> wrote in message <hve28s$33d$1(a)fred.mathworks.com>...
>
> > Single line indeed! I knew there was an accumarray solution in there somewhere, but I will cop to dpb's excuse!
>
> Oh common, you guys are hopeless about admiring the poesy of accumarray. I'm going to sleep.
>
> Bruno

There is a problem with A(end) = 0, if the solution is to be that general. I am sure you see the fix!
From: Bruno Luong on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <hve2po$6op$1(a)fred.mathworks.com>...

> There is a problem with A(end) = 0, if the solution is to be that general. I am sure you see the fix!

A fix that prevails a single-line is not quite straight-forward but possible.

y = subsref(cumsum(accumarray(cumsum([1; A(:,2)]), diff([0; A(:,1); inf]))),substruct('()',{1:sum(A(:,2))}))

Bruno
From: Bruno Luong on

> % 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))

Matt, any quick fix for A=[1 0;2 0]

??? Attempted to access CS(0); index must be a positive integer or logical.

Bruno
From: Matt Fig on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message
> Matt, any quick fix for A=[1 0;2 0]
>
> ??? Attempted to access CS(0); index must be a positive integer or logical.
>
> Bruno

Always the danger in pointing out a flaw in Bruno's code! ;') It seem like a little IF check would need to be put in.
First  |  Prev  | 
Pages: 1 2 3
Prev: Smooth surface mesh/grid
Next: Memory Usage