필터 지우기
필터 지우기

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

조회 수: 2 (최근 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에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by