Index exceeds matrix dimensions.

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 Kommentare

KSSV
KSSV am 21 Feb. 2017
Check size of Vl, your n and a loop numbers should not exceed the length of Vl
hariharan ilango
hariharan ilango am 21 Feb. 2017
Bearbeitet: Guillaume am 21 Feb. 2017
My previous lines of code
for n = 1:k
if M(n) == 1
a = 'High speed';
fprintf('M(%d) = %s \n',n,a);
else
b = 'Low Energy';
fprintf('M(%d) = %s \n',n,b);
end
end
fprintf('Number of High speed tasks = %d \n',h);
fprintf('Number of Low Energy tasks = %d \n',l);
for n = 1:h
fprintf('Vh(%d) = %d \n',n,Vh(n));
end
for n = 1:l
fprintf('Vl(%d) = %d \n',n,Vl(n));
end
if sHS < h
while sHS < h
max = Vh(1);
a = 1;
for n = 2:h
if Vh(n) > Vh(a)
max = Vh(n);
a = n;
else
max = Vh(a);
a = a;
end
end
fprintf('Maximum hiding value from HS = Vh(%d) = %d \n',a,Vh(a));
l = l+1;
let(l) = [a];
h = h-1;
hst(a) = [];
end
else
g = 'Sufficient memory is available';
disp(g);
end
fprintf('Number of High speed tasks = %d \n',h);
fprintf('Number of Low Energy tasks = %d \n',l);
for n = 1:h
fprintf('High Speed Task(%d) = %d \n',n,hst(n));
end
for n = 1:l
fprintf('Low Energy Task(%d) = %d \n',n,let(n));
end
b = 0;
Walter Roberson
Walter Roberson am 21 Feb. 2017
Bearbeitet: Walter Roberson am 21 Feb. 2017
h and l and Vh and Vl are undefined.
prompt = 'Enter individual tasks memory location \n High Speed = 1 \n Low Energy = 0 \n';
fprintf('M(%d) = \n',n);
M(n) = input (prompt);
if M(n) == 1
h = h+1;
prompt = 'Enter the hiding value of high speed tasks \n';
fprintf('V(%d) = \n',h);
Vh(h) = input (prompt);
hst(h) = n;
else
l = l+1;
prompt = 'Enter the hiding value of low speed tasks \n';
fprintf('V(%d) = \n',l);
Vl(l) = input (prompt);
let(l) = n;
end
end

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 21 Feb. 2017

2 Stimmen

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 Kommentare

hariharan ilango
hariharan ilango am 22 Feb. 2017
Bearbeitet: Walter Roberson am 22 Feb. 2017
prompt = 'Enter individual tasks memory location \n High Speed = 1 \n Low Energy = 0 \n';
fprintf('M(%d) = \n',n);
M(n) = input (prompt);
if M(n) == 1
h = h+1;
prompt = 'Enter the hiding value of high speed tasks \n';
fprintf('V(%d) = \n',h);
Vh(h) = input (prompt);
hst(h) = n;
else
l = l+1;
prompt = 'Enter the hiding value of low speed tasks \n';
fprintf('V(%d) = \n',l);
Vl(l) = input (prompt);
let(l) = n;
end
end
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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 21 Feb. 2017

Kommentiert:

am 22 Feb. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by