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

조회 수: 5 (최근 30일)
E
E 2019년 2월 25일
편집: Samy Alkhayat 2021년 4월 1일
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

채택된 답변

Star Strider
Star Strider 2019년 2월 25일
편집: Star Strider 2019년 2월 25일
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
E
E 2019년 2월 25일
It worked! thank you so much for your help!
Star Strider
Star Strider 2019년 2월 25일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

madhan ravi
madhan ravi 2019년 2월 25일
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
E
E 2019년 2월 25일
Thank you
Aarpita Sood
Aarpita Sood 2020년 1월 20일
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.

댓글을 달려면 로그인하십시오.


nag raj
nag raj 2020년 7월 16일
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)));
  댓글 수: 1
Star Strider
Star Strider 2020년 7월 16일
Post this as a new question. It is not an Answer to this thread.

댓글을 달려면 로그인하십시오.


Samy Alkhayat
Samy Alkhayat 2021년 4월 1일
편집: Samy Alkhayat 2021년 4월 1일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by