In: Mechanical Engineering
The array price given below contains the price in dollars of a certain stock over 10 days. Use MATLAB to determine how many days the price was above $20.
price = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29]
We have been provided with an array that contains the prices for 10 days. We need to determine for how many days the array prices were above $20.
We can create a Boolean array which will contain 1 if the array price is greater than $20; otherwise it will contain 0. We can use the sum function to calculate the number.
The MATLAB code is given below:
Input:
x = [19,18,22,21,25,19,17,21,27,29];
array = x>20;
sum(array)
Output:
We find that on 6 days the price was greater than $20.
We find that on 6 days the price was greater than $20.