MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Sara on 20 Jun 2012

for the code below I received the error index exceeds.. I do not know how can I fix it.

x = zeros( 2,length(v0) ) ; for k = 1:length(v0) A = [ factor1(:,k) factor2(:,k)]; b = -rhs(:,k); x(:,k) = A\b ; //end of for

length v0 = 2501

A\b is 2 by 2 matrix but x(:,k) is 2 by 2501.

*No products are associated with this question.*

Answer by per isakson on 20 Jun 2012

Do you use the debugger?

- Put a break on the line that causes the error or
- Tools | Debug | Stop if Error
- Run the code
- At the break inspect the values of the index

Walter Roberson on 20 Jun 2012

A zero index would generate the error about indices needing to be positive integers, not the error about exceeding matrix dimensions.

per isakson on 20 Jun 2012

Thanks Walter; fixed. I try to make the point that better asking the debugger than Answer.

## 3 Comments

Direct link to this comment:http://www.mathworks.nl/matlabcentral/answers/41655#comment_85235

Which line is generating the error?

What are the sizes of factor1 and factor2 and rhs ?

Direct link to this comment:http://www.mathworks.nl/matlabcentral/answers/41655#comment_85241

the sizes of factor1 and factor2 are 2 by 2501.

by the line x (:,k) = A\b , it has error.

Direct link to this comment:http://www.mathworks.nl/matlabcentral/answers/41655#comment_85252

That doesn't sound like an "index exceeds" error, that sounds like an error about RHS having more non-singleton dimensions than the LHS has.

Please show the exact error.

If A\b is a 2 x 2 matrix, then were you only expecting a 2 x 1 matrix as the result of the A\b operation? Or you only care about one of the rows or one of the columns? Or you want to store all 4 results and do that in a single column ?