# Index exceeds matrix dimensions Error.

5 views (last 30 days)
Vilnis on 7 Mar 2014
Commented: Vilnis on 7 Mar 2014
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?

Image Analyst on 7 Mar 2014
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/

Vilnis on 7 Mar 2014
This is what it says: Name Size Bytes Class Attributes
weather 1x1 3219032 struct
Name Size Bytes Class Attributes
current 1x4 32 double
While I may not understand it quite right, I dont think weather whould be 1x1, since the csv file I'm loading it from is 11434 rows tall...
Image Analyst on 7 Mar 2014
Well you forgot to attach it, and I'm quitting for the day so I won't get a chance to look at it until tomorrow. Did you use csvread() and did you look at the link I directed you to? I expect you'll have it solved by tomorrow. Let me know if you haven't and attach the file. If you have then mark my answer as "Accepted" because I alerted you to the fact that weather is not the array you thought it was.
Vilnis on 7 Mar 2014
Yep, I figured it out. Thanks!