Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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 Kommentar
dpb
dpb am 19 Aug. 2015
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 19 Aug. 2015
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.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by