problem with extra entries in a matrix!

Hello, I would like to fill a matrix but I have a problem in filling it, because I get some extra entries even though I have initialized my matrix to the required size. Please find the problematic loop bellow, and hope you can help, thank you.
year = 1;
i = 0;
Nbrtot = 6;
MyResults = zeros(Nbrtot, 1);
for l = 1:year
for nbrrepetiton = 0:l
for NoYes = [0 1]
for semester = [1 2]
i = i + 1;
MyResults(i) = i;
end
end
end
end
Note: using this code My matrix MyResults has 2 extra entries.

4 件のコメント

KSSV
KSSV 2017 年 5 月 22 日
You have to explain the loop...what you are trying to save?
Ano
Ano 2017 年 5 月 22 日
it's kind of tracking the holidays of the employees during for example a year how many times they took a break 'nbrrepetiton', and that break was it for holidays or health problems 'NoYes' and at which semester of the year they took it first or second.
KSSV
KSSV 2017 年 5 月 22 日
How you are sure that Nbrtot = 6 ?
Ano
Ano 2017 年 5 月 22 日
what do you mean ?? for me Nbrtot is the total number of element that should be in the whole summation it is fixed but I have divided my problem into those loops in order to simplify it . any suggestions how to make the entries exactly as specified ?

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

 採用された回答

Stephen23
Stephen23 2017 年 5 月 22 日
編集済み: Stephen23 2017 年 5 月 22 日

0 投票

for l = 1:year % one iteration
for nbrrepetiton = 0:l % two iterations
for NoYes = [0 1] % two iterations
for semester = [1 2] % two iterations
Each nested loop multiplies the total iterations by its own number of iterations, so we get:
1x2x2x2 = 8
So we find that MATLAB is doing exactly what you told it to do: eight iterations in total. There is no reason to expect six iterations here.

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

Ano
2017 年 5 月 22 日

編集済み:

2017 年 5 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by