フィルターのクリア

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

2 ビュー (過去 30 日間)
Cary
Cary 2015 年 8 月 19 日
回答済み: Star Strider 2015 年 8 月 19 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by