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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Shounak Shastri
Shounak Shastri el 28 de Mzo. de 2018
Editada: Shounak Shastri el 28 de Mzo. de 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 comentarios
Madelynne Jones
Madelynne Jones el 28 de Mzo. de 2018
Thank you so much! This was exactly what I was looking for.
Peter Perkins
Peter Perkins el 29 de Mzo. de 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)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by