Matlab 在不改变代码点的情况下克服'exceed matrix dimensions'



下面这个矩阵是一个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

你的循环将总是超过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

最新更新