我在Matlab中的代码有问题,这很奇怪。我有一个for循环,其中包含一些关于一些日期的语句,下面是代码:
for i=1:data_length_Added
if (month(dateAdded(i)) ~= 0) && (month(dueDate(i)) ~= 0) && (month(dueDate(i)) == month(datePublished(i)))
[~,idx]=ismember(month(dueDate(i)),kpit(:,1),'rows');% finds the position of an element from the array in the array for months
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx,3) = kpit(idx,3) + 1;
idx = idx + 1;
else
if (month(dateAdded(i)) ~= 0) && (month(datePublished(i)) == (month(dueDate(i)) + 1)) && month(dueDate(i)) ~= 0
[~,idx]=ismember(month(dueDate(i)),kpit(:,1),'rows');
[~,idx1]=ismember(month(datePublished(i)),kpit(:,1),'rows');
year1 = year(datePublished(i));
weight = (day(datePublished(i)))/eomday(year1,month(datePublished(i)));
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx1,2) =kpit(idx1,2) + overdue(i)*weight;
kpit(idx,3) = kpit(idx,3)+ 1;
kpit(idx1,3) = kpit(idx1,3)+ 1;
else
if (month(dateAdded(i)) ~= 0) && (month(datePublished(i)) ~= 0) && (month(datePublished(i)) > (month(dueDate(i))+1)) && (month(dueDate(i)) ~= 0)
[~,idx]=ismember(month(datePublished(i)),kpit(:,1),'rows');
year1 = year(datePublished(i));
weight = day(datePublished(i))/eomday(year1,month(datePublished(i))); %Should count all overdue days for full months and weighted overdue days for not full months
kpit(idx,2) = overdue(i)*weight;
kpit(idx,3) = kpit(idx,3) + 1;
for j=month(dueDate(i)) : (month(datePublished(i))-1)
[~,idx]=ismember(month(dueDate(i)),kpit(:,1),'rows');
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx,3) = kpit(idx,3)+ 1;
j = j + 1;
month(dueDate(i)) = month(dueDate(i)) + 1;
end
else
if (month(dateAdded(i)) ~= 0) && month(dueDate(i)) ~= 0 && month(datePublished(i)) == 0
[~,idx]=ismember(month(today),kpit(:,1),'rows');
year1 = year(today);
weight = day(today)/eomday(year1,month(today)); %Should count all overdue days for full months and weighted overdue dats for not full months
kpit(idx,2) = overdue(i)*weight;
kpit(idx,3) = kpit(idx,3) + 1;
for j=month(dueDate(i)) : (month(today)-1)
[~,idx]=ismember(month(dueDate(i)),kpit(:,1),'rows');
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx,3) = kpit(idx,3)+ 1;
j = j + 1;
month(dueDate(i)) = month(dueDate(i)) + 1;
end
end
end
end
end
end
所以,问题是我成功地通过循环运行了代码,而问题发生在我在第367行的第一个日期之后(在此之前我只有零(。基本上,我对实际进入第一个嵌套循环的第一个日期和第一个日期之前的零没有问题,问题发生在那之后。你认为是什么可能导致我收到的下标索引错误的问题?谢谢:((
在此处输入图像描述
我已经设法解决了我的问题。我不得不改变我对if语句中空的日期的称呼方式。首先,我为空日期设置了零,但之后我将它们改为NaN,这样我就可以检查当我有NaN时该做什么。这是工作代码:
mDateAdd = month(dateAdded);
mDatePubl = month(datePublished);
mDueDate = month(dueDate);
mToday = month(today);
dateAdded(dateAdded == 0) = NaN;
datePublished(datePublished == 0) = NaN;
dueDate(dateAdded == 0) = NaN;
for i=1:data_length_Added
if mDueDate(i) == mDatePubl(i) && ~isnan(dateAdded(i)) && ~isnan(dueDate(i))
[~,idx]=ismember(mDueDate(i),kpit(:,1),'rows');% finds the position of an element from the array in the array for months
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx,3) = kpit(idx,3) + 1;
idx = idx + 1;
else
if ~isnan(dateAdded(i)) && (mDatePubl(i)) == (mDueDate(i) + 1) && ~isnan(dueDate(i))
[~,idx]=ismember(mDueDate(i),kpit(:,1),'rows');
[~,idx1]=ismember(mDatePubl(i),kpit(:,1),'rows');
year1 = year(datePublished(i));
weight = round((day(datePublished(i)))/eomday(year1,mDatePubl(i)));
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx1,2) =kpit(idx1,2) + overdue(i)*weight;
kpit(idx,3) = kpit(idx,3)+ 1;
kpit(idx1,3) = kpit(idx1,3)+ 1;
else
if ~isnan(dateAdded(i)) && ~isnan(datePublished(i)) && mDatePubl(i) > mDueDate(i)+1 && ~isnan(dueDate(i))
[~,idx]=ismember(mDatePubl(i),kpit(:,1),'rows');
year1 = year(datePublished(i));
weight = (day(datePublished(i))/eomday(year1,mDatePubl(i))); %Should count all overdue days for full months and weighted overdue days for not full months
kpit(idx,2) = kpit(idx,2) + round(overdue(i)*weight);
kpit(idx,3) = kpit(idx,3) + 1;
for j = mDueDate(i) : (mDatePubl(i)-1)
[~,idx]=ismember(mDueDate(i),kpit(:,1),'rows');
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx,3) = kpit(idx,3)+ 1;
j = j + 1;
mDueDate(i) = mDueDate(i) + 1;
end
else
if ~isnan(dateAdded(i)) && ~isnan(dueDate(i)) && isnan(datePublished(i))
[~,idx]=ismember(mToday,kpit(:,1),'rows');
year1 = year(today);
weight = round(day(today)/eomday(year1,mToday)); %Should count all overdue days for full months and weighted overdue dats for not full months
kpit(idx,2) = overdue(i)*weight;
kpit(idx,3) = kpit(idx,3) + 1;
for j=mDueDate(i) : (mToday-1)
[~,idx]=ismember(mDueDate(i),kpit(:,1),'rows');
kpit(idx,2) =kpit(idx,2) + overdue(i);
kpit(idx,3) = kpit(idx,3)+ 1;
j = j + 1;
mDueDate(i) = mDueDate(i) + 1;
end
end
end
end
end
end