From: Michael Teo on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i422qr$7jq$1(a)fred.mathworks.com>...
> "Michael " <mteo(a)empiricap.com> wrote in message <i3vv6j$kfu$1(a)fred.mathworks.com>...

> My guess is that you mean that if a = 1, that puts things in an 'on' mode which causes c to become a 1 and stay that way until a value in b puts it in the 'off' mode by becoming -1. When the latter happens, c will turn to 0 and stay there until a again brings it back to the 'on' mode. Matt has already pointed one weakness. How does it behave when a "wants" an 'on' mode and b an 'off' mode at the same time? Another weakness is, what mode do you wish c to start in initially if both a and b start with 0's?
>
> I will assume that if both a and b contradict each other, then you will leave the mode the way it was. Also it is assumed that initially the mode starts at 'off'.
>
> n = length(a);
> c = zeros(size(a));
> A = a==1;
> B = b~=-1;
> m = false; % Mode is 'off' initially
> for k = 1:n
> m = A(k)&&B(k)||A(k)&&m)||B(k)&&m;
> c(k) = +m;
> end
>
> Roger Stafford

Thanks you pple for helping and clarifying. And special thanks to Roger for the details elaboration.

Q1. How does it behave when a "wants" an 'on' mode and b an 'off' mode at the same time?
A1: The mode will switch. i.e., if previous mode is 'on' and 'a=1' and 'b=-1', c will become 0. Similar, if previous mode is 'off' and 'a=1' and 'b=-1', c will become 1.

Q2. What mode do you wish c to start in initially if both a and b start with 0's?
A2: The mode will always assume to start as '0'. If both a and b start with '0', c will be 0.

The for-loop solution is acceptable but i am looking for a more efficient computation, such as using vectors manipulation.

Thanks, any advices or questions is appreciated.
From: Roger Stafford on
"Michael Teo" <mteo(a)empiricap.com> wrote in message <i42c85$p2k$1(a)fred.mathworks.com>...
> Thanks you pple for helping and clarifying. And special thanks to Roger for the details elaboration.
>
> Q1. How does it behave when a "wants" an 'on' mode and b an 'off' mode at the same time?
> A1: The mode will switch. i.e., if previous mode is 'on' and 'a=1' and 'b=-1', c will become 0. Similar, if previous mode is 'off' and 'a=1' and 'b=-1', c will become 1.
>
> Q2. What mode do you wish c to start in initially if both a and b start with 0's?
> A2: The mode will always assume to start as '0'. If both a and b start with '0', c will be 0.
>
> The for-loop solution is acceptable but i am looking for a more efficient computation, such as using vectors manipulation.
>
> Thanks, any advices or questions is appreciated.
- - - - - - - - - - - -
I have corrected the logic to always do a switch in mode when a is 1 at the same time b is -1. As it happens, the logic is made simpler by this change.

n = length(a);
c = zeros(size(a));
A = a==1;
B = b~=-1;
m = false; % Mode is 'off' initially
for k = 1:n
m = A(k)&&~m||B(k)&&m;
c(k) = +m;
end

I think the prospects for a vectorized solution with this kind of problem are dim. Such is generally the case when iterative procedures generate variables that depend on their own prior values such as is the case with mode m here. Don't despair. It often happens in matlab's newer versions that for-loops outdo vectorized solutions in speed, especially if the vectorization requires numerous elaborate steps to accomplish its goal.

Roger Stafford