MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by Brian on 26 Mar 2013

The arrays below contain the price in dollars of three stocks over 10 days.

price_A=[19,18,22,21,25,19,17,21,27,29]

price_B=[22,17,20,19,24,18,16,25,28,27]

Price_C=[17,13,22,23,19,17,20,21,24,28]

Use matlab to determine how many days the price of stock A was above both the price of stock B and the price of stock C.

I'm having trouble structuring the answer to this question. I'm not sure how to do it with all three opposed to just two. I could use a hint a step in the right direction. I was thinking that I need to use find(price_A>..... but after that im not sure what needs to be done.

*No products are associated with this question.*

Answer by Walter Roberson on 26 Mar 2013

Hint:

condition1 & condition2

Brian on 26 Mar 2013

I still don't understand how the conditionals work here. I tried: if (price_B>Price_C) then (price_A>Price_B).

Image Analyst on 26 Mar 2013

You need to and the conditionals, and then sum them.

price_A > price_B give a logical array for when A cost more than B.

price_A > price_C give a logical array for when A cost more than C.

To find out when A was more than BOTH of them, you need to AND.

A_more_than_both = (price_A > price_B) & (price_A > price_C)

That is an array of 1's and 0's, like 0 0 1 1 0 1 0 0 1 1 0 or whatever. To find out how many days total there are, you need to sum that.

days_above_both = sum(A_more_than_both)

## 3 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/68617#comment_138936

In addition to Walter's hint, look at the following

so relational operators (like

>) "return" vectors of logicals (true/false symbolized by 1/0), and you can work with such vectors, typically use logical operators on them (e.g&).Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/68617#comment_138940

I still don't understand, I read the section and there is nothing remotely similar to this question that is being asked. Your hints aren't really helping me.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/68617#comment_138943

What you have to do is to test multiple conditions simultaneously. I'm showing in my comment that the output of the test (using a relational operator) of one condition is a vector of logicals. On the other side, Walter gave you a hint about performing a logical AND between vectors of logicals ..

To illustrate a little more, if you want to test if

xis greater than 5 but smaller than 7, you can use nested IF statementswhich is what you tried to do as you mention in your reply to Walter (and which is a good first approach). Now if

x=10for example, look at what you obtain with the above testsboth 1 and 0 here are logicals (true, false), as mentioned in my comment, and you can perform logical operations on logicals, so if you wanted to combined the two tests into a single one, hence reducing the nested IF statement to a single IF statement, you could build the following statement

Play a bit with these concepts and you'll see that they truly apply to your case.