How can I create a loop for this equation?

1 回表示 (過去 30 日間)
Fatima Khaled
Fatima Khaled 2019 年 7 月 4 日
回答済み: Torsten 2019 年 7 月 4 日
function [level] = initiallevel(t,dem)
total = sum(dem(1:t));
level = total/t;
end
I need to then create a vector that consists of [L4 all the way to L12] so made up of 9 rows and 1 column.
>> L4 = initiallevel(4,demand);
>> L5 = initiallevel(5,demand);
>> L6 = initiallevel(6,demand);
>> L7 = initiallevel(7,demand);
>> L8 = initiallevel(8,demand);
>> L9 = initiallevel(9,demand);
>> L10 = initiallevel(10,demand);
>> L11 = initiallevel(11,demand);
>> L12 = initiallevel(12,demand);

採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 7 月 4 日
With a loop:
demand = rand(100,1);
L = zeros(9,1);
for t = 4:12
L(t-3) = initiallevel(t,demand)
end
Using arrayfun
demand = rand(100,1);
L = arrayfun(@(t) initiallevel(t,demand),4:12)';

その他の回答 (1 件)

Torsten
Torsten 2019 年 7 月 4 日
L = cumsum(demand);
L = L./(1:numel(L));
L = L(4:12)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by