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

7 views (last 30 days)
I kept getting this error message when I try to create a for loop to generate a 1x25 array:
"Index in position 2 exceeds array bounds (must not exceed 1)"
The array should be
A = [1 2 3....25]
Any help would be greatly appreciated. This is what I have so far,
A = [1]
for i = 2:25
A(:,i) = A(:,i+1)
end

Accepted Answer

Star Strider
Star Strider on 25 Feb 2019
Edited: Star Strider on 25 Feb 2019
You have assigned ‘A’ as a scalar. Preallocating is a good idea, however this would likely be more effective:
A = ones(1,24);
for i = 1:25
A(:,i+1) = A(:,i)+1;
end
  4 Comments

Sign in to comment.

More Answers (3)

madhan ravi
madhan ravi on 25 Feb 2019
A=zeros(1,25); %preallocation is important
A(1)=1;
for k=2:25
A(:,i)=A(:,i-1)+1;
end
A
Note: This task doesn’t require loop , just vectorization should do the job.
A=1:25;
Bit it’s pretty clear that you want to get in practice with a for loop.
  2 Comments
Aarpita Sood
Aarpita Sood on 20 Jan 2020
I am using KNN classifier
k_nn=ind(:,1:k);
nn_index=k_nn(:,1);
These lines show error "Index in position 2 exceeds array bounds (must not exceed 22)"
Any clarifications would be appreciated.

Sign in to comment.


nag raj
nag raj on 16 Jul 2020
function [Qr, lolb] = solveP3(Qr,Xr)
global K M H alpha beta0 delta_t Dmax B sigma_2 Lamda Pk q0 qF Sk F_1 w u tolerance
Ql = Qr;
l = 0; tol = tolerance;
Lo = [];
for l = 1: 1000
Qlt = repelem(Ql,1,1,K);
J = H^2 + sum( (Qlt - w).^2 ,1);
J = reshape(J, [M,K] );
I = F_1*Pk*dB2dec(beta0)*(alpha/2)*log2(exp(1)) ./ (J .* (dB2dec(sigma_2) * dB2dec(Lamda) * J .^(alpha/2) + F_1*Pk*dB2dec(beta0)));
A = log2(1 + (F_1*Pk*dB2dec(beta0)) ./ (dB2dec(sigma_2) * dB2dec(Lamda) * J .^(alpha/2)) );
cvx_begin quiet
variables Q(2,M)
variables lo(1)
expression LO(K)
maximize (sum(lo))
subject to:
for k = 1 : K
LO(k) = 0;
for m = 1 : M
Rlb = A(m,k) - I(m,k) * (sum_square_abs( Q(:,m) - u(:,k))) + I(m,k) * (sum_square_abs( Ql(:,m) - u(:,k)));
LO(k) = LO(k) + Xr(m,k) * Rlb;
end
(1/(Sk/(B*delta_t))) * LO(k) >= lo; %Constraint (17)
end
for m = 2: M
norm(Q(:,m) - Q(:,m-1)) <= Dmax;
end
Q(1,1) == q0(1);
Q(2,1) == q0(2);
Q(1,M) == qF(1);
Q(2,M) == qF(2);
cvx_end
Lo = [Lo;sum(lo)];
Ql = Q;
if (l >= 2) &&(Lo(l) - Lo(l-1) < tol)
break;
end
end
Qr = Q;
lolb = Lo(l);
end
function [dB] = dec2dB(dec)
dB = 10*log10(dec);
end
function [dec] = dB2dec(dB)
dec = 10.^(dB/10);
end
same problem
Index in position 2 exceeds array bounds (must not exceed 1).
Error in solveP3 (line 41)
Rlb = A(m,k) - I(m,k) * (sum_square_abs( Q(:,m) - u(:,k))) + I(m,k) * (sum_square_abs( Ql(:,m) - u(:,k)));

Samy Alkhayat
Samy Alkhayat on 1 Apr 2021
Edited: Samy Alkhayat on 1 Apr 2021
Hello,
I have the same error when I run this piece of code to come up with a property of a mixture of 2 components
rDCN=randsample(DCN,Ncomponents);
v = rand(1,7)';
rv = randsample(v,Ncomponents);
for i = 1:Ncomponents
D = rDCN(:,i).*rv(:,i);
end
please advise

Community Treasure Hunt

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

Start Hunting!