mPulse
mPulse

Reputation: 23

Matlab error: Index exceeds matrix dimensions (Looking a single element of a vector only)

Here is my code:

m is the length of the vector y.

1 c=1; 
2 cMax=1;
3  
4 while c<=m
5
6       if abs((y(c)-y(c+1)))>0.001
7           cMax=cMax+1;
8       end
9
10  c=c+1;
11 end

Essentially, vector y is a vector with a set of integers which has been organised from smallest to greatest, I'm trying to find out how many different values of y there are.

I'm comparing the current value of y to the next value of y and saving how many changes there are in cMax.

I've changed the if logic statement a few times. It has been:

if y(c)~=y(c+1)

And I reversed the if statement like this:

if y(c)==y(c+1)
 %do nothing
 ;
else 
 cMax=cMax+1; 

I'm not sure what I'm doing wrong but the error message is always the same:

error: A(I): Index exceeds matrix dimension.

error: called from:

error: C:\Users\dickweed\Documents\Study\Machine Learning\Tutorials\ex3\oneVsA ll.m at line 57 [6], column 3 [way before if statement]

error: C:\Users\dickweed\Documents\Study\Machine Learning\Tutorials\ex3\ex3.m at line 58 [7] , column 14 [after the letter c]

I've bolded where the code indicates the errors in my supplied code.

The columns are wacky, that may be because of the text editor I'm using, but I'm assuming the actual columns mean before the if statement and before the end statement.

Any help would be greatly appreciated.

PS. I'm actually using Octave, and Notepad++. The language is exactly the same, for all intents and purposes, as Matlab hence why I've labelled it Matlab.

Upvotes: 2

Views: 3893

Answers (1)

Hugues Fontenelle
Hugues Fontenelle

Reputation: 5425

I think that your index exceeds matrix dimensions.

Specificaly in the following line:

y(c)-y(c+1)

on the last iteration, when c=m, the second term y(c+1) tries to access an element that doesn't exist.

Suggestion: change your stop condition to:

c<m

Upvotes: 2

Related Questions