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:
Elegant hysteresis calculation help

Subject: Elegant hysteresis calculation help

From: dclist

Date: 20 Mar, 2010 20:51:47

Message: 1 of 5

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.

Subject: Elegant hysteresis calculation help

From: Bruno Luong

Date: 20 Mar, 2010 21:23:03

Message: 2 of 5

% 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

Subject: Elegant hysteresis calculation help

From: Sadik

Date: 20 Mar, 2010 22:25:05

Message: 3 of 5

Here is another one:

i = find(a);
k = a(i(cumsum(a~=0)));

Subject: Elegant hysteresis calculation help

From: dclist

Date: 20 Mar, 2010 23:41:06

Message: 4 of 5

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

Thanks, very nice.

Subject: Elegant hysteresis calculation help

From: Sadik

Date: 21 Mar, 2010 00:17:05

Message: 5 of 5

I'd like to acknowledge Bruno since my solution is inspired from his.

Enjoy.

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