From: Michael on 12 Aug 2010 01:01 hi, i have 2 vectors input as follow: a = [0;1;1;0;1;0;0;0;0;1] b = [0;0;0;0;0;0;0;-1;0;0] and the output i am trying to generate is c = [0;1;1;1;1;1;1;0;0;1] whereby the logic is as follow: 1. if a is '1', c='1'. ('ON' mode) 2. if a is in 'ON' mode, c will always be '1'. 3. if b is '-1', c='0'. ('OFF' mode) 4. if b is in 'OFF' mode, c will always be '0'. may i ask if there is any matlab function that does this logic? or is there any efficient ways of getting the output instead of using for-loop? thanks in advance for sharing! cheers! Michael
From: Michael on 12 Aug 2010 01:15 sry for multiple submission, kindly delete this thread. thx mod.
From: Sean on 12 Aug 2010 09:08 "Michael " <mteo(a)empiricap.com> wrote in message <i3vv6j$kfu$1(a)fred.mathworks.com>... > hi, > > i have 2 vectors input as follow: > > a = [0;1;1;0;1;0;0;0;0;1] > b = [0;0;0;0;0;0;0;-1;0;0] > > and the output i am trying to generate is > c = [0;1;1;1;1;1;1;0;0;1] > > whereby the logic is as follow: > 1. if a is '1', c='1'. ('ON' mode) > 2. if a is in 'ON' mode, c will always be '1'. > 3. if b is '-1', c='0'. ('OFF' mode) > 4. if b is in 'OFF' mode, c will always be '0'. > > may i ask if there is any matlab function that does this logic? > or is there any efficient ways of getting the output instead of using for-loop? > C = > thanks in advance for sharing! > > cheers! > Michael Your 'c' example doesn't comply with your rules. Why is c(1) ==0 and c(4) ~=0? I think you're looking for something like this though c = a | (b>0); %| is the logical 'or' symbol.
From: Matt Fig on 12 Aug 2010 09:28 Your example does not follow the rules you set out. Also, your rules don't seem clear enough to decide in every case. For example, you say that if a is ON then c is always 1, and if b is OFF then c is always 0. But what is c when BOTH a is ON AND b is OFF? A consistent set of logical rules will not have such ambiguities in it!
From: Roger Stafford on 12 Aug 2010 20:15 "Michael " <mteo(a)empiricap.com> wrote in message <i3vv6j$kfu$1(a)fred.mathworks.com>... > hi, > > i have 2 vectors input as follow: > > a = [0;1;1;0;1;0;0;0;0;1] > b = [0;0;0;0;0;0;0;-1;0;0] > > and the output i am trying to generate is > c = [0;1;1;1;1;1;1;0;0;1] > > whereby the logic is as follow: > 1. if a is '1', c='1'. ('ON' mode) > 2. if a is in 'ON' mode, c will always be '1'. > 3. if b is '-1', c='0'. ('OFF' mode) > 4. if b is in 'OFF' mode, c will always be '0'. > > may i ask if there is any matlab function that does this logic? > or is there any efficient ways of getting the output instead of using for-loop? > > thanks in advance for sharing! > > cheers! > Michael - - - - - - - - - - - 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
|
Next
|
Last
Pages: 1 2 Prev: The speed of the random number generator vs importing a text Next: vector computation |