I need help debugging a code. Please help me.

4 visualizaciones (últimos 30 días)
Alice Lin
Alice Lin el 6 de Feb. de 2022
Comentada: Voss el 6 de Feb. de 2022
I need with getting only postive integers in part d. If you look at the outputs section. You will see that it only contains the positive integers instead of both the positive and negative integers.
Suppose output
% Profits = {{'FACE' 'BSH'}; [60.5 215.25]}
%% Variables to be used
% Inputs
% Stocks - cell array containing
% top element - cell array containing single row of stock abbreviations (character arrays)
% bottom element - matrix 3 x N matrix of purchase price (row 1), current price (row 2), and number of stocks (row 3)
% Outputs
% CurrentHight - character array of stock with the highest current price
% Cheapest - character array of stock with the lowest purchase price
% Best - cell array containing character array of stocks which have increased by 25% in value
% Profits - 2 x 1 cell array containign
% element 1 - single-row cell array of stocks found in Best
% element 2 - single-row vector contain profit from selling each stock in Best
% Total Profits - scalar containing total profit from selling all stocks in Best
%% Inputs
% Generate_Stocks % This script generates the cell array Stocks.
% Test Case 1
% Stocks = {{'APL' 'BBT' 'ZZT'}; [29.76 11.69 38.06; 23.83 15.77 37.25; 58 48 3]};
% Test case output:
% CurrentHigh = 'ZZT'
% Cheapest = 'BBT'
% Best = {'BBT'}
% Profits = {{'BBT'}; 195.84}
% TotalProfits = 195.84
% Test Case 2
Stocks = {{'GOOG' 'FACE' 'ZOM' 'BSH'}; [2.65 8.95 32.96 27.09; 2.52 11.37 27.85 37.34; 49 25 86 21]}
% Test case output:
% CurrentHigh = 'BSH'
% Cheapest = 'GOOG'
% Best = {'FACE' 'BSH'}
% Profits = {{'FACE' 'BSH'}; [60.5 215.25]}
% TotalProfits = 275.75
%% Program
% Write your program here
%Problem a)
% currentHigh will hold the max value of the 2nd row of the 2nd element of
% the stocks cell array which holds the current price of the stock
[MaxPower,MaxLoc] = max(Stocks{2}(2,:));
CurrentHigh = Stocks{1}{MaxLoc};
%Problem b)
% cheapest holds the min value of the 1st row for the 2nd element of the
% stocks cell array which holds the purchase price of the stock
[MinPower,MinLoc] = min(Stocks{2}(1,:));
Cheapest = Stocks{1}{MinLoc};
%Problem c)
% creates a 1x1 cell array which stores all the stocks that have increased
% by more 25% - calculating the difference between the current price and
% the purchase price and checking whether the difference is more than 25%
% of the purchase price, and then projecting the logical array to the
% character array of stock abbreviations to get the desired abbreviations
Best = (Stocks{1}(Stocks{2}(2,:)-Stocks{2}(1,:) > 0.25.*Stocks{2}(1,:)))
%Problem d)
% calculating the profit for each of the stock by subtracting purchase price
% from current price and multiplying with total number of stocks present to
% get the total profits
B = (Stocks{2}(2,:) - Stocks{2}(1,:)).*(Stocks{2}(3,:))
Profits = {(Best);B}
%Problem e)
% getting the total profit from the current Profit
TotalProfits = sum(Profits{2})
  2 comentarios
David Hill
David Hill el 6 de Feb. de 2022
It would be helpful is you provided an example of your cell array.
Alice Lin
Alice Lin el 6 de Feb. de 2022
Hello,
For the Profits it only contains the positive integers such as % Profits = {{'FACE' 'BSH'}; [60.5 215.25]}, but my code contains these integers and also Profits =
2×1 cell array
{1×2 cell }
{[-6.3700 60.5000 -439.4600 215.2500]}

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 6 de Feb. de 2022
Make a variable (I call it is_best) that stores the information about whether each stock is among the best stocks. (I've made it a logical vector, but you could also make it a vector of indices using find().) Then use that variable to index into Stocks{1} when creating Best and to index into each row of Stocks{2} when creating B:
% Stocks = {{'APL' 'BBT' 'ZZT'}; [29.76 11.69 38.06; 23.83 15.77 37.25; 58 48 3]};
Stocks = {{'GOOG' 'FACE' 'ZOM' 'BSH'}; [2.65 8.95 32.96 27.09; 2.52 11.37 27.85 37.34; 49 25 86 21]};
is_best = Stocks{2}(2,:)-Stocks{2}(1,:) > 0.25.*Stocks{2}(1,:)
is_best = 1×4 logical array
0 1 0 1
Best = Stocks{1}(is_best)
Best = 1×2 cell array
{'FACE'} {'BSH'}
B = (Stocks{2}(2,is_best) - Stocks{2}(1,is_best)).*(Stocks{2}(3,is_best))
B = 1×2
60.5000 215.2500
Profits = {Best;B}
Profits = 2×1 cell array
{1×2 cell } {[60.5000 215.2500]}
  2 comentarios
Alice Lin
Alice Lin el 6 de Feb. de 2022
Thank you very much.
Voss
Voss el 6 de Feb. de 2022
You are welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by