How do I make my data from a while loop into a table of vectors?

1 vue (au cours des 30 derniers jours)
Madelynne Jones
Madelynne Jones le 28 Mar 2018
Commenté : Peter Perkins le 29 Mar 2018
I would like to take all of my data that I get from this while loop and make it into a table, with each year's data being made into its own row.
wsalary=63554;
msalary=66097;
year=2018
while year<=2038
wsalary=wsalary+(wsalary*.05)
msalary=msalary+(msalary*.05)
salarydiff=msalary-wsalary
eratio=wsalary/msalary
paygap=(msalary-wsalary)/msalary
year=year+1
end

Réponse acceptée

Shounak Shastri
Shounak Shastri le 28 Mar 2018
Modifié(e) : Shounak Shastri le 28 Mar 2018
"How do I make my data from a while loop into a table of vectors?"
I have modified the code you gave in your question.
wsalary(1)=63554;
msalary(1)=66097;
year=2018
temp=2;
while year<=2038
wsalary(temp)=wsalary(temp-1)+(wsalary(temp-1)*.05)
msalary(temp)=msalary(temp-1)+(msalary(temp-1)*.05)
salarydiff(temp)=msalary(temp)-wsalary(temp)
eratio(temp)=wsalary(temp)/msalary(temp)
paygap(temp)=(msalary(temp)-wsalary(temp))/msalary(temp)
year=year+1;
temp=temp+1;
end
table(wsalary', msalary', salarydiff',eratio',paygap')
Best of Luck!
  2 commentaires
Madelynne Jones
Madelynne Jones le 28 Mar 2018
Thank you so much! This was exactly what I was looking for.
Peter Perkins
Peter Perkins le 29 Mar 2018
Madelynne, you might consider that your loop is completely unnecessary. For one thing, three of those five variables can be computed from the other two after making the table, for example,
t.salarydiff = t.msalary - wsalary
The other two take a little thought, but are also simple:
wsalery = cumprod(repmat(1.05,21,1)) * 63554;
msalery = cumprod(repmat(1.05,21,1)) * 66097;
t = table(wsalary,msalary)
Also, if you are using a recent version of MATLAB, consider using a timetable:
year = (2018:2038)';
tt = timetable(year,wsalary,msalary)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by