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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by