# I want to create a table from this for loop for each different value the overtimesalary and salary

2 ビュー (過去 30 日間)
Jorge Tejada 2017 年 10 月 5 日

S= 12;
t = (20:50);
Salary = S*t;
Overtimesalary = S*40+((S*1.5)*(t-40));
for t = (20:50)
if t > 40
Overtimesalary = S*40+((S*1.5)*(t-40));
else
salary = S*t;
end
end

サインインしてコメントする。

### 回答 (2 件)

Stephen 2017 年 10 月 5 日
One problem you're probably having is that you are overwriting the Overtimesalary variable each loop. I would restructure the code a bit since you've got that same equation in two places, using an anonymous function for calculating pay. I would also eliminate the selection between functions, instead implanting a single function for pay that works whether overtime is worked or not.
Here's what I would do:
S= 12;
PayFunc = @(t,S) S*t + min(((S*0.5)*(t-40)),0);
payTable = zeros(50-19,2);
for t = 20:50
rowPtr = t-19;
payTable(rowPtr ,1) = t;
payTable(rowPtr ,2) = PayFunc(t,S)
end
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Andrei Bobrov 2017 年 10 月 12 日
S = 12;
t = 20:50;
your_salary = S*t;
your_salary(t > 40) = S*(40+1.5*(t-40));

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by