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:
vector computation

Subject: vector computation

From: Michael

Date: 12 Aug, 2010 05:01:07

Message: 1 of 7

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

Subject: vector computation

From: Sean

Date: 12 Aug, 2010 13:08:07

Message: 2 of 7

"Michael " <mteo@empiricap.com> wrote in message <i3vv6j$kfu$1@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.

Subject: vector computation

From: Matt Fig

Date: 12 Aug, 2010 13:28:09

Message: 3 of 7

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!

Subject: vector computation

From: Roger Stafford

Date: 13 Aug, 2010 00:15:23

Message: 4 of 7

"Michael " <mteo@empiricap.com> wrote in message <i3vv6j$kfu$1@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

Subject: vector computation

From: Michael Teo

Date: 13 Aug, 2010 02:56:05

Message: 5 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i422qr$7jq$1@fred.mathworks.com>...
> "Michael " <mteo@empiricap.com> wrote in message <i3vv6j$kfu$1@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.

Subject: vector computation

From: Roger Stafford

Date: 13 Aug, 2010 05:54:05

Message: 6 of 7

"Michael Teo" <mteo@empiricap.com> wrote in message <i42c85$p2k$1@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

Subject: vector computation

From: Michael Teo

Date: 20 Aug, 2010 09:23:06

Message: 7 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i42mlt$eg4$1@fred.mathworks.com>...
> "Michael Teo" <mteo@empiricap.com> wrote in message <i42c85$p2k$1@fred.mathworks.com>...

> - - - - - - - - - - - -
> 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

Thank you so much for yr advices and efforts. I guess you r rite, not all problems can be vectorized. :)

Tags for 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