MATLAB Answers


error-index exceeds matrix dimension

Asked by FIR
on 17 Apr 2012
In the following code i get error a s
P1 = [-1 -1 2 2; 0 5 0 5];
Tar = [0  ;1 ]
  for i=1:10
         test=(indices==i);trains= ~test
tst =  (indices==i);
val = (indices== mod(i+1,10));
trn = ~[tst,val]; 
         out = round(sim(net,P(:,test)));    
Index exceeds matrix dimensions.
Error in cfour (line 58)
please help


Log in to comment.


3 Answers

Answer by Walter Roberson
on 17 Apr 2012
 Accepted Answer

That code is going to generate an error unless "indices" is of length 1 exactly. If it is longer than 1, then "test" and "train" will be longer than 1, and would then be too long to use as logical vectors against the columns of the single-column Tar array.

  1 Comment

Walter can u please tell ho wto process newff with cross validation for fisheriris data set plz,i am getting many errors while processing as stated below in my comments

Log in to comment.

Answer by Andreas Goser
on 17 Apr 2012


throws an error in the first run, as Tar has no second dimension. Probably you mean:



I get error as

Error using trainlm (line 109)
Inputs and targets have different numbers of samples.

Error in network/train (line 106)
[net,tr] = feval(net.trainFcn,net,X,T,Xi,Ai,EW,net.trainParam);

Error in cfour (line 60)

You just asked why you got this error. Now you know ;-)

I may know more about MATLAB, but hope fully you know more about neural networks... The message "Inputs and targets have different numbers of samples." That sounds like an actionable error message, isn't it?

yes Andreas,hopefully i dont know more about neural networks ,can u please telll how to correct this error please ,and also please answer for my post "edit-error in classsifier"

Log in to comment.

Answer by Greg Heath
on 22 Apr 2012

1. The input and target matrices must have the same number of columns:

Tar = [ 0 0 1 1 ]

[ I N ] = size( P1) % [ 2 4 ] [ O N ] = size(Tar) % [ 1 4 ]

k = 10


2. a. It doesn't make sense to use k > N

   b.Instead of using CROSSVALIND from the Bioinformatics TBX, the algorithm 
might be more portable if you use CROSSVAL from the Statistics TBX.

3. trains= ~test

Rename. TRAINS is a MATLAB function.

Hope this helps.



[ O N ] = size(Tar) % [ 1 4 ]

Should be on a new line.


Typical nontrivial classification examples should have classes with
many more I/O training pairs than input dimensions.

For the FisherIris example/demo (c = 3, I = 4, N = 150).

Although that ratio is

N/(3*4) = 12.5,

the scatter plot in the PetalLength/PetalWidth plane indicates
that the 3 classes are linearly separable with two hidden nodes.

Hope this helps.


Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today