# "Index in position 2 exceeds array bounds (must not exceed 1)"

12 views (last 30 days)
Jem Jem on 10 Aug 2022
Commented: Jem Jem on 11 Aug 2022
%Index in position 2 exceeds array bounds (must not exceed 1).
I keep facing this error message when I try to create a for loop array , to generate P,E,K are 4-by-1 matrix :
Any help would be greatly appreciated. This is what I currently have
%Error in untitled4 (line 46)
% L(m,t) = P(m,t)+E(m,t)-E(m,t-1)+K(m,t)-K(m,t-1);
M=4;
T=24;
L=zeros(4,25);
P=[60; 63; 65; 70];
E=[4; 6; 8; 11];
K=[16; 18; 22; 24];
for m=1:M
for t=2:T
L(m,t) = P(m,t)+E(m,t)-E(m,t-1)+K(m,t)-K(m,t-1);
end
end
figure
plot(0:24,L(1,:), 0:24,l(2,:), 0:24,L(3,:), 0:24,L(4,:))
xlabel('t')
ylabel('L m')
legend('MG1', 'MG2', 'MG3', 'MG4')

Torsten on 10 Aug 2022
P, E and K are (4x1) arrays.
In the loop assignment
L(m,t) = P(m,t)+E(m,t)-E(m,t-1)+K(m,t)-K(m,t-1);
you treat the arrays as if they were (4x25).
This leads to the error message.
To prevent this, define P,E and K as (4x25) instead of (4x1).
##### 1 CommentShowHide None
Jem Jem on 11 Aug 2022
its worked
Thanks

R2020a

### Community Treasure Hunt

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

Start Hunting!