# How to solve: Attempted to access inc(:,1); index out of bounds because size(inc)=[0,0]?

2 views (last 30 days)
Jeroen Klein on 25 Feb 2015
Edited: Stephen on 25 Feb 2015
I am trying to model a BLP model in MATLAB. For this, I need to compute a non-linear part of utility, which is done via the function below. The problem I encounter is that the error above keeps coming up, whereas the matrix inc is 2301 x 50. Other inputs are the following dimensions, in case that helps: v = 2301 x 200 (namely, because X contains 4 characteristics, and thus we need 4 v's per observation), K = 4, n = 2301, X = 2301 x 4, price1=2301 x 1. I hope you will be able to help me. Thank you very much in advance.
function f = mufunc(X, sigma)
%This function computes the non-linear part of the utility
%This is based on the code written by Aviv Nevo, 1998
global nsm v inc price1 mu
[n K] = size(X);
for i = 1:nsm
v_i = v(:,i:nsm:K*nsm);
inc_i = inc(:,i);
mu(:,i) = (-sigma(1).*price1./inc_i+(X.*v_i*(sigma(2:end))'));
end
f = mu;

Stephen on 25 Feb 2015
Edited: Stephen on 25 Feb 2015
Although you state that "the matrix inc is 2301 x 50" this is clearly not true at some point as the error message clearly states: "index out of bounds because size(inc)=[0,0]".
Sadly inc is a global variable.
Which makes this situation a classic example of why using globals is a bad idea: it means that inc can be changed somewhere at some point by another function and there is no easy way to know when this happens:
To quote from the first link, Doug Hull: "I have never seen MATLAB code where globals were the right thing to do". And Matt Fig: "Those beginners (at my work) who use these constructs end up calling me to come fix their mess a few months down the road". And Jan Simon: "these commands cause more problems than they solve". Yep, indeed.
So inc's size changes at some point when the code runs, and you have two choices:
• Use the debugging tools or whatever other tools to track down when this happens, and then why. And then try to fix it and also prevent something similar happening again. Note that you can set conditional breakpoints!
• Rewrite your code to not use globals (as much as possible), thus ensuring that random unexpected variable changes such as this do not occur.
I think you can figure out which of these is the better solution.
Jeroen Klein on 25 Feb 2015
Regards,