Overcoming 'index exceeds matrix dimensions' without changing methodology of code

1 次查看(过去 30 天)
This matrix below is a 25x7 matrix. Basically what I'm doing is taking a start date and an end date, and adding 1 to the start date, and subtracting 1 from the end date. The problem is when I get to the last (25th) iteration, where my index exceeds the matrix dimensions. Here the start date is 20081210 and I need to get 20081211. How can I do so without changing the methodology of my code? Thank you.
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 个评论
dpb
dpb 2015-8-19
The loop would run w/o error if you simply used length(matrix)-1 as the upper limit.
But, it doesn't look like this makes any sense to be using a loop here as you are unless this is a shortened example and there's something else not shown as all the values of j other than the last are going to waste.
What's the real objective?; in most instances like this with Matlab you can likely do away with the loop entirely--again unless there's much else that isn't shown in play.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2015-8-19
I don’t entirely undrestand what you’re doing, or what you’re adding or subtracting 1 from. Here are two ways of dealing with dates, the first using datenum and the second creating date vectors:
datevcts = [2008*ones(25,1), 12*ones(25,1), 11*ones(25,1), [[1:25]' zeros(25,2)]];
matrix_dn = datenum(datevcts);
matrix_dv = datevec(matrix_dn);
That may give us a place to begin sorting this.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by