Dear all,
the situation is the following: I have a projectlength of 3 years with an assumed number of hours of 10. For each hour the yield is calculated with a certain function (which I simplify in this example with the number 2). As the projectlength and hournumber should stay variable, I would like to first calculate the yield in each hour of the year (helper(k)) and then append the solutions of each projectyear below each other. So the yield should look like this:
I got the following so far, however, it does not work.
hournum = 10;
projectlength = 3;
yield = [];
for p=1:projectlength
for k = 1:hournum
helper(k)=2;
end
yield(p) = [yield(p); {helper(k)}];
end
Error Message: Index exceeds the number of array elements (0).

2 件のコメント

Jan
Jan 2021 年 1 月 18 日
Whenever you write "does not work", append the error message or a details description of the difference between your expectations and the obtained results.
Maria Hart
Maria Hart 2021 年 1 月 18 日
Ok, thank you. I have added the error message.

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

 採用された回答

Jan
Jan 2021 年 1 月 18 日
編集済み: Jan 2021 年 1 月 18 日

0 投票

hournum = 10;
projectlength = 3;
yield = cell(1, projectlength); % Pre-allocate as cell
helper = zeros(1, hournum); % Pre-allocate as vector
for p = 1:projectlength
for k = 1:hournum
helper(k) = 2;
end
yield{p} = helper;
end

3 件のコメント

Maria Hart
Maria Hart 2021 年 1 月 18 日
編集済み: Maria Hart 2021 年 1 月 18 日
I have tried your solution, but I get a 1x3 cell with thee times the 2 instead a 30x1 vector.
Maria Hart
Maria Hart 2021 年 1 月 18 日
編集済み: Maria Hart 2021 年 1 月 18 日
Thank you for your answer. If you would like to have instead a simple vector with all numbers beneath each other and no cell (I would like to plot all data), what would I do?
Maria Hart
Maria Hart 2021 年 1 月 18 日
Found a way with cell2mat!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2021 年 1 月 18 日

コメント済み:

2021 年 1 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by