Can someone help me out with how to structure the answer to this question?
2 views (last 30 days)
Show older comments
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.
3 Comments
Cedric
on 26 Mar 2013
Edited: Cedric
on 26 Mar 2013
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 x is greater than 5 but smaller than 7, you can use nested IF statements
if x > 5
if x < 7
... do something
end
end
which is what you tried to do as you mention in your reply to Walter (and which is a good first approach). Now if x=10 for example, look at what you obtain with the above tests
>> x = 10 ;
>> x > 5
ans =
1
>> x < 7
ans =
0
both 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
if (x > 5) & (x < 7)
.. do something
end
Play a bit with these concepts and you'll see that they truly apply to your case.
Answers (1)
Walter Roberson
on 26 Mar 2013
Hint:
condition1 & condition2
3 Comments
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)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!