From: dclist on 20 Mar 2010 16:51 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 20 Mar 2010 17:23 % 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 20 Mar 2010 18:25 Here is another one: i = find(a); k = a(i(cumsum(a~=0)));
From: dclist on 20 Mar 2010 19:41 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 20 Mar 2010 20:17 I'd like to acknowledge Bruno since my solution is inspired from his. Enjoy.
|
Pages: 1 Prev: xcorr2 vs. normxcorr2 Next: Parallel computing numerical solutions to system of ODEs |