Attempted to access x(2); index out of bounds because numel(x)=1. But I'm using a matrix?

28 views (last 30 days)
I'm just trying to execute a simple for loop, but I keep getting this error "Attempted to access x(2); index out of bounds because numel(x)=1.
Error in Quiz (line 11) x = x(i)" I don't understand what the error means. Any help would be appreciated!
x = [22.5 45 67.5 90]
for i=1:4
x = x(i)
a = a(x)
c = c(x)
alpha = alpha(x)
mu(a,c)
end

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 4 May 2013
Edited: Azzi Abdelmalek on 4 May 2013
After one itteration
for i=1:4
x = x(i)
the length of x becomes equal to 1, then x(2) does not exist

More Answers (3)

Iman Ansari
Iman Ansari on 4 May 2013
Hi.
You change your x in first loop :
x = x(i) ====> x=22.5
after this x became a number.
  5 Comments
Valentina Marincevic Petracic
Edited: Stephen23 on 5 Jan 2017
same question:
function [QRS] = AF2(EKG)
QRS=zeros(1,8191);
a=max(EKG);
Xth=0.4*a; %amplitude threshold
Y0=zeros(1,8191);
Y1=zeros(1,8191);
Y2=zeros(1,8191);
for n=1:8191
if (EKG(n)>0)
Y0=EKG(n);
elseif (EKG(n)<0)
Y0=-EKG(n);
end
end
%NP filtar:
for k=1:8191
y=Y0(k);
if ( y>= Xth)
Y1(k)=y;
else
Y1(k)=Xth;
end
end
%prva derivacija:
for n=2:8190
Y2= Y1(n+1)- Y1(n-1);
end
%detekcija qrs-a:
for i=0: 8191
if((Y2(i))>0.7)
QRS(i)=1;
end
end
end

Sign in to comment.


Aaina
Aaina on 7 Aug 2018
How can I solve this problem?
Attempted to access C(38,1); index out of bounds because size(C)=[37,38].
Error in Without_DG (line 94) if ((C(f,i)==-1)&&(k==1));
%% MATLAB Program
k=1;
for i=1:no
if ((C(f,i)==-1)&&(k==1));
f=i;
g(j,e)=i;
e=e+1;
k=3;
end
end
  3 Comments
Aaina
Aaina on 7 Aug 2018
Edited: Aaina on 7 Aug 2018
Alright.... Thank you so much.... the input data is correct, but the code was incorrect, i tried to change the data by using the same code which need to access different network of distribution system... which from radial system into mesh system which the network more complex
Aaina
Aaina on 7 Aug 2018
I want the code can access mesh system by just adding the mesh data, but it seems doesnt work

Sign in to comment.


deeba naqvi
deeba naqvi on 15 Oct 2020
Even I am getting the same error..plz some body help.....The programm code is.....
lb = [0,0,0,-inf];
Aeq = [1 1 1 0];
beq = 1;
x0 = [0.25 0.25 0.25 8];
fun = @(x) -x(4);
A = [];
b = [];
ub = [];
nonlinear = @Alteredplay2;
nonlcon = nonlinear(x);
[X, FVAL] = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, nonlcon)
function [C, Ceq] = Alteredplay2(x)
%x0 = [0.25 0.25 0.25 2];
C(1)= x(4)-((1-(5.8/8))^(0.5)*(0.1/8)^(0.5)*(1-(4.4/8))^(0.5)*(1.7/8)^(0.5))^x(1)*((1-(2/8))^(0.5)*(2.9/8)^(0.5)*(1-(1.4/8))^(0.5)*(5.8/8)^(0.5))^x(2)*((1-(3.4/8))^(0.5)*(0.1/8)^(0.5)*(1-(5.2/8))^(0.5)*(2.7/8)^(0.5))^x(3);
C(2)= x(4)-((1-(6.1/8))^(0.5)*(0.6/8)^(0.5)*(1-(3.8/8))^(0.5)*(1.7/8)^(0.5))^x(1)*((1-(2.4/8))^(0.5)*(3.1/8)^(0.5)*(1-(1.3/8))^(0.5)*(5.6/8)^(0.5))^x(2)*((1-(7.1/8))^(0.5)*(0.3/8)^(0.5)*(1-(3.6/8))^(0.5)*(0.8/8)^(0.5))^x(3);
Ceq= [];
end
and the error is....
Attempted to access x(4); index out of bounds because
numel(x)=2.
Error in Alteredplay2 (line 3)
C(1)=
x(4)-((1-(5.8/8))^(0.5)*(0.1/8)^(0.5)*(1-(4.4/8))^(0.5)*(1.7/8)^(0.5))^x(1)*((1-(2/8))^(0.5)*(2.9/8)^(0.5)*(1-(1.4/8))^(0.5)*(5.8/8)^(0.5))^x(2)*((1-(3.4/8))^(0.5)*(0.1/8)^(0.5)*(1-(5.2/8))^(0.
Error in ExceptPlay2Altered (line 10)
nonlcon = nonlinear(x);

Community Treasure Hunt

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

Start Hunting!