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

1 次查看(过去 30 天)
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

采纳的回答

Shounak Shastri
Shounak Shastri 2018-3-28
编辑:Shounak Shastri 2018-3-28
"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 个评论
Peter Perkins
Peter Perkins 2018-3-29
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by