How do I prompt a user to input a number of values for their future inputts?

조회 수: 2 (최근 30일)
So right now, the code I have makes the user input up to an infinite number of values for the cash flow matrix and the intrest rates. How do I allow the user to first input the number of inputs they need, then individually ask the values for each yearly cash flow as well as the interest rates?
clc;
clear;
close all;
A= input('Input cash flow matrix in the form "[yr1 yr2 yr3 yr...] \n"-->'); %Defines a initially after buying the machine
n=length(A);
for i =1:n
x(i) = i-1;
end
PW= 0;
int=input('Input various intrest rates in the form [a b c...]\n-->');
bar(x,A,0.25)
hold off
%Equation for PW analysis
for j= 1:length(int)
PW=0;
for t= 1:n
PW=PW+A(t)*int(j)^(-x(t));
PWA(j,t)=PW;
end
end
%plots CF diagram
figure(1)
hold on
plot(x,PWA,'-x','LineWidth',2)
legend('CF diagram','Location','southeast')
Current output:
Input cash flow matrix in the form "[yr1 yr2 yr3 yr...]"
-->[-5000 2000 2000 1000] *note these are random numbers I input
Input various intrest rates in the form [a b c...]
-->[1.01 1.02] *note these are random numbers I input

채택된 답변

Geoff Hayes
Geoff Hayes 2019년 4월 26일
편집: Geoff Hayes 2019년 4월 26일
Mallard - use a for loop. For example,
numYears= input('please enter number of years: ');
cashFlows = zeros(numYears, 1);
interestRates = zeros(numYears, 1);
for k = 1:numYears
cashFlows(k) = input(sprintf('input cash flow for year %d: ', k));
interestRates(k) = input(sprintf('input interest rate for year %d: ', k));
end
The above assumes that the user enters in the same number of interest rates as cash flow values.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cash Flows에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by