Matlab克服超过矩阵维度'不改变代码点

时间:2015-08-19 19:17:56

标签: matlab

下面的矩阵是25x7矩阵。基本上我正在做的是开始日期和结束日期,并在开始日期加1,从结束日期减去1。问题是当我到达最后一次(第25次)迭代时,我的索引超出了矩阵维度。这里的开始日期是20081210,我需要获得20081211.如何在不改变代码方法的情况下这样做?谢谢。

 for i = 1:length(matrix)
      plus1=matrix(i+1,1);
      minus1=matrix(i,2)-1;
      [~,startIdx]=ismember(plus1,date); % index days in between entry date and exit date
      [~,cutoffIdx]=ismember(minus1,date); % index days in between entry date and exit date
      j=date(startIdx:cutoffIdx);
  end

1 个答案:

答案 0 :(得分:0)

您的循环将始终超过matrix的长度,因此错误输出。

for i = 1:length(matrix)
    plus1 = matrix(i + 1, 1); % What happens when i == length(matrix)?
    % i + 1 = 26 which is greater than the rows in matrix

您需要将循环限制更改为

for i = 1:length(matrix) - 1
    plus1 = matrix(i + 1, 1); % the max value of i + 1 will be the length of matrix

我不知道其他方面是否正确,因为您尚未完全定义matrixdate