MATLAB: Index exceeds matrix dimensions Error.

dimensionserrorexceedsindexmatrixneeds to learn debugging

Hi!
I'm working on a script and while it works with smaller vectors, I can't get it to work with the one I need it to work with which is considerably larger.
Index exceeds matrix dimensions.
Error in avalanchePredictor (line 31)
dist(i) = sqrt((weather(i,4) - current(1))^2 + (weather(i,5) -
current(2))^2 + (weather(i,6) - current(3))^2 + (weather(i,7) -
current(4))^2);
This is the exact error message that I'm getting and below is the code line 31 is around for reference.
dist = zeros(1, 100000);
weather = importdata('weather.csv');
for i = 1:11434
dist(i) = sqrt((weather(i,4) - current(1))^2 + (weather(i,5) - current(2))^2 + (weather(i,6) - current(3))^2 + (weather(i,7) - current(4))^2);
end
for i = 1:11434
fprintf(fid,'%10.0f %10.0f %10.0f %10.3f \n', weather(i,(1:3)), dist(i));
end
fid = fclose('all');
As I said, I think I'm trying to loop it for too long or something (I'm not very good at MATLAB to begin with…), and I tried following some fo the suggested steps, but to no avail. Can anybody help me out?

Best Answer

What is 11434? Is that size(weather, 1)? If so, use the size function to be more flexible in case your file is not exactly 11434 rows long. Before the for loop, put this line:
whos weather
whos current
Tell us what it says. My guess is that either current is not 4 elements long or weather is not 11434 rows tall or 7 columns wide. Finally, this is something that you have to look at because once you do you'll be solving this much much faster than posting and waiting for one of us to answer: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/