Index exceeds matrix dimensions

1 view (last 30 days)
Maroulator
Maroulator on 11 Aug 2014
Edited: Image Analyst on 11 Aug 2014
I have the code below, but I don't understand why it is that my index exceeds matrix dimensions.
i=1;
for min=0:60:240
hour(i)=min(i+1)/60;
i=i+1;
end
I also tried the code below to work around the error, but I am still getting it. Any takers?
min=0:60:240;
for i=1:length(min)
hour(i)=min(i+1)/60;
end

Accepted Answer

Image Analyst
Image Analyst on 11 Aug 2014
Edited: Image Analyst on 11 Aug 2014
min() is a built in function. You've done something very bad by using it as your variable name.
But since you've destroyed the min function, it's now a variable with length of length(min). So what index are you at when i = length(min)? You have min(length(min)+1) which is past the end of your badly-named variable. Just say:
minIndex = 0:60:240;
for i=1:length(minIndex)
hour(i) = minIndex(i)/60;
end
Or
minIndex = 0:60:240;
for i = minIndex
hour(i) = i / 60;
end
Or, even better
minIndex = 0:60:240;
hour = minIndex / 60;

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!