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
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!