Index in position 2 exceeds array bounds (must not exceed 1).
6 次查看(过去 30 天)
显示 更早的评论
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
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
更多回答(3 个)
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 个评论
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
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 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!