Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
% 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!

Respuesta aceptada

Image Analyst
Image Analyst el 10 de Nov. de 2022
Editada: Image Analyst el 10 de Nov. de 2022
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))
  2 comentarios
Image Analyst
Image Analyst el 10 de Nov. de 2022
You're welcome. Thanks for accepting. I had to make a correction to the lowPrice line (I had taken column 5 instead of 6).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by