From: dclist on
I want to implement a hysteresis on a vector of values.
Equivalently, I want to replace zero values with the previously
occurring non-zero value.

For example,

[1 1 1 0 0 0 -1 -1 0 -1 1 0] => [1 1 1 1 1 1 -1 -1 -1 1 1]

I'm looking for a solution more elegant than a for loop but it is
eluding me.

Performance is not critical.
From: Bruno Luong on
% Elegant? Not sure.

a=[1 1 1 0 0 0 -1 -1 0 -1 1 0]

i = find(a);
[b b] = histc(1:length(a),[i inf]);
a = a(i(b));

Bruno
From: Sadik on
Here is another one:

i = find(a);
k = a(i(cumsum(a~=0)));
From: dclist on
On Mar 20, 6:25 pm, "Sadik " <sadik.h...(a)gmail.com> wrote:
> Here is another one:
>
> i = find(a);
> k = a(i(cumsum(a~=0)));

Thanks, very nice.
From: Sadik on
I'd like to acknowledge Bruno since my solution is inspired from his.

Enjoy.