Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Shounak Shastri
Shounak Shastri il 28 Mar 2018
Modificato: Shounak Shastri il 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 Commenti
Madelynne Jones
Madelynne Jones il 28 Mar 2018
Thank you so much! This was exactly what I was looking for.
Peter Perkins
Peter Perkins il 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)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by