Index exceeds matrix dimensions.
10 ビュー (過去 30 日間)
古いコメントを表示
if sLE < l
while sLE < l
max = Vl(1);
a = 1;
for n = 2:l
if Vl(n) > Vl(a) ***
max = Vl(n);
a = n;
else
max = Vl(a);
a = a;
end
end
In line marked with *, index exceeds matrix dimensions error is shown
4 件のコメント
Walter Roberson
2017 年 2 月 21 日
編集済み: Walter Roberson
2017 年 2 月 21 日
h and l and Vh and Vl are undefined.
回答 (1 件)
Guillaume
2017 年 2 月 21 日
a = a;
Pointless line of code that does nothing.
something = [a];
Use of the concatenation operator, [], with only one argument. Again pointless.
As per KSSV's comment if you get an index exceeds matrix dimension error, one of n or a is too big. It can't be a with your code, so it must be n. The maximum value of n is h. So h must be too big. We don't know where h is coming from (nor what the variable contains since the name is meaningless), so can't tell you why it is too big but surely you can figure this out yourself and if not, you can use the debugger.
In any case, rather than using constants whose values you don't know why not tell the loop to explicitly loop over the real number of elements:
for n = 2:numel(Vh) %n is guaranteed not to exceed the number of elements since we tell the loop to end at that number
But of course, there's already a function to calculate the maximum of a vector, it's the max function which you could use if you didn't name your variable max.
%this one line replaces the whole for loop:
[~, a] = max(Vh); %and do not use max as a variable name
2 件のコメント
Guillaume
2017 年 2 月 22 日
As said, use the debugger to find out where your code goes wrong. It will immediately become clear if you watch the value of l as you advance through the code line by line.
It's not clear which order the various pieces you've posted come in. However, I see that l is supposed to be the length of Vl, yet you increase it in your while sHS < h loop. This may be the reason for your error. Again, if you'd used the debugger you'd have seen this immediately and got an answer quicker than asking in a forum.
Also, as said, an easy way to ensure that your loop does not go above the number of elements in a vector is by asking the vector how many elements it actually has rather than using a variable which may or may not be up to date:
for n = 1:numel(Vl)
would prevent the error (but wouldn't solve the fact that l no longer represent the number of elements in Vl).
参考
カテゴリ
Help Center および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!