How can I format with fprintf to show 2 seperate listsas the output?

9 vues (au cours des 30 derniers jours)
Luis Velez
Luis Velez le 11 Sep 2020
Commenté : Luis Velez le 11 Sep 2020
Introductory course into matlab assignment. Im trying to list the n and A outputs into column list but can get the format right. They mix in between each other.
function [A] = Loanpayment( p,i);
n = input('Number of payments: ');
%A calcualtes the annual payment to be made
%P= Initial loan amount
%n= Annual payments agreed upon, in this case 5 payments were agreed
%i= Interest rate on P, loan amount
A = (p).* ((i.*(1+i).^n)./((1+i).^n -1));
z = linspace(A,0,n);
y = [1:n];
fprintf('Number of Payments Payment amount\n')%List of Number of...
%payments and amount per payment
fprintf('%g %25.3f \n',z)
fprintf('%g %25.3f \n',y)
end

Réponse acceptée

Ameer Hamza
Ameer Hamza le 11 Sep 2020
Try this
function [A] = Loanpayment( p,i)
n = input('Number of payments: ');
%A calcualtes the annual payment to be made
%P= Initial loan amount
%n= Annual payments agreed upon, in this case 5 payments were agreed
%i= Interest rate on P, loan amount
A = (p).* ((i.*(1+i).^n)./((1+i).^n -1));
z = linspace(A,0,n);
y = [1:n];
fprintf('Number of Payments Payment amount\n')%List of Number of...
%payments and amount per payment
fprintf('%g\t%25.3f \n', [y; z])
end
  2 commentaires
Walter Roberson
Walter Roberson le 11 Sep 2020
Note for the above purpose that it is important in the fprintf() that y and z are row vectors rather than column vectors. The [y;z] then gives a 2 x something array with the first element in each column being y and the second element in the column being z. Then fprintf() always goes "down" columns -- the %g would be applied to the first entry in memory, the %25.3f would be applied to the second element in memory, and that is the end of the % formats so %g would be re-used for the third element in memory which is the second y, %25.3f would be re-used for the 4th element in memory, which would be the second z, and so on.
You should also have a look at compose() which has a more natural behaviour.
Luis Velez
Luis Velez le 11 Sep 2020
Thank you, this is what I wanted it to do. Much appreciated!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by