Need help identifying values from a matrix after identifying two values from its vector.

1 次查看(过去 30 天)
% this is a matrix sorting stocks
Example = [5 27 2022 820 827 785 820; % first row is month, 2nd is day, 3rd is year, 4th is how much the price opened for that day, 5th is the high for that day, 6th is the low for that day, and 7th is what the price closed on that day.
5 28 2022 813 824 801 805;
5 29 2022 808 835 804 822;
5 30 2022 809 835 801 812;
5 31 2022 811 834 802 823;
6 1 2022 858 899 854 898;
6 2 2022 894 908 871 881;
6 3 2022 888 897 880 882;
6 4 2022 889 895 858 864;
6 5 2022 877 886 866 885]
xMonth = input('Month: ')
xDay = input ('Day: ')
Hello all, I am trying to understand the basics of writing a script for evaluating stocks. Here I want to identify the opening price, closing price, highest price, and lowest price after inputting the month and day. I would like to make these outputs:
fprintf('On month %d day %d, this stock opened at %d and closed at %d.\n' xMonth, xDay, , )
fprintf('The highest price for that day was %d and lowest was %d.\n' , )
In other words, I believe I am trying to figure out how to write a code that scans the 1st and 2nd row for those values of my matrix so I can use that vector. I am assuming I'll have to use a for loop but am unsure how to write it. I would appreciate any help on this!

采纳的回答

Image Analyst
Image Analyst 2022-11-10
编辑:Image Analyst 2022-11-10
Try this:
% This is a matrix sorting stocks
% The first row is month, 2nd is day, 3rd is year,
% 4th is how much the price opened for that day,
% 5th is the high for that day, 6th is the low for that day,
% and 7th is what the price closed on that day.
Example = [5 27 2022 820 827 785 820;
5 28 2022 813 824 801 805;
5 29 2022 808 835 804 822;
5 30 2022 809 835 801 812;
5 31 2022 811 834 802 823;
6 1 2022 858 899 854 898;
6 2 2022 894 908 871 881;
6 3 2022 888 897 880 882;
6 4 2022 889 895 858 864;
6 5 2022 877 886 866 885]
xMonth = input('Month: ');
xDay = input ('Day: ');
openingPrice = Example(:, 4);
closingPrice = Example(:, 5);
lowPrice = Example(:, 6);
highPrice = Example(:, 7);
% Find the row that matches
row = find(Example(:, 1) == xMonth & Example(:, 2) == xDay)
fprintf('On month %d day %d, this stock opened at %d and closed at %d.\n', ...
xMonth, xDay, openingPrice(row), closingPrice(row))
fprintf('The highest price for that day was %d and lowest was %d.\n' , ...
lowPrice(row), highPrice(row))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by