我在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行的第一个日期之后(在我只有零之前)。基本上,我在实际进入第一个嵌套循环的第一个日期和第一个日期之前的零没有问题,此后发生了问题。您认为这可能导致我收到的下标索引错误的问题是什么? 谢谢:)))
答案 0 :(得分:0)
我设法解决了我的问题。我必须更改在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