Why do I get "Array indices must be positive integers or logical values" error when using "angle" function?
3,135 views (last 30 days)
Show older comments
MathWorks Support Team
on 26 Mar 2018
Commented: Walter Roberson
on 6 Jun 2022
Why do I get "Array indices must be positive integers or logical values" error when using "angle" function?
>> z = 22+i
>> a = angle(z)
>> z =
22.0000 + 1.0000i
ERROR: Array indices must be positive integers or logical values.
Accepted Answer
MathWorks Support Team
on 20 May 2021
Edited: MathWorks Support Team
on 20 May 2021
Based on the error message, it seems like you have a variable with the name "angle" stored in your workspace that overshadows the "angle" function
11 Comments
More Answers (34)
Adarsh Vijayan
on 25 Apr 2020
y=1:10;
>> s=0;
>> m=0;
>> while m==0
if y(s)==9
disp("gotvha")
m=45;
else
s=s+1;
end
end
Array indices must be positive integers or logical values.
pls tell what is the error here ?
7 Comments
Mansoor Ahmad
on 11 Apr 2022
@Walter Roberson thanks alot, actually I knew that multiplication rule, it was just a typographical mistake.
Candy Day
on 7 Sep 2019
Help Please;
why do i keep getting this error?
Array indices must be positive integers or logical values.
'roots' appears to be both a function and a variable. If this is unintentional, use 'clear roots' to remove the variable 'roots' from the workspace.
p=[13, 182, -184, 2503];
roots(p)
2 Comments
CarlaN
on 6 May 2020
Hi,
I'm encountering the same problem..:
n = 1;
for i= n:0.1:2
[tri, trap, rect] = motionSpeed(n(i), 0.5);
fprintf('For n=%.6f, tri = %.6f, trap = %.6f, rect = %.6f', n(i), tri, trap, rect);
end
I obtain a result for n=1, but for the following values of n, I get the error message "Array indices must be positive integers or logical values". Does anyone have any ideas where this could come from?
Thanks
1 Comment
Walter Roberson
on 6 May 2020
for i= n:0.1:2
n starts out as 1. On the first iteration, i would start at as 1. Inside the loop you refer to n(i) which would be n(1) which fits with n being a scalar.
Then on the second iteration of the loop, i would become approximately 1.1. n(i) would then be attempting n(1.1) but it is not permitted to index by a non-integer.
If you were thinking "second n to go along with second i value", then you would have the problem that n is a scalar, so the only valid indices for it are false, true, [] (empty) and 1 .
I speculate you want:
n = 1 : 0.1 : 2;
for i = 1 : length(n)
[tri, trap, rect] = motionSpeed(n(i), 0.5);
fprintf('For n=%.6f, tri = %.6f, trap = %.6f, rect = %.6f', n(i), tri, trap, rect);
end
Faham Zaidi
on 10 May 2020
q=imread('pic.png');
p=uint8(floor(0:255)/2);
b2=p(q);
imshow(q),figure,imshow(b2)
t1=0.667*[0:96];
t2=2*[97:160]-128;
t3=0.6632*[161:255]+85.8947;
p=uint8(floor([t1 t2 t3]));
figure;
plot([0:255],p,'.'),axis tight
figure;
b3=p(q);imshow(b3)
Array indices must be positive integers or logical values.
Error in lab3_1 (line 3)
b2=p(q);
Can someone help running this code ???
4 Comments
Hilina Workneh
on 22 May 2020
h_ref = 0.3;
k = 0.5;
h=0.2;
if h < h_ref
u = k(h_ref - h);
else
u = 0;
end
Array indices must be positive integers or logical values.
why i keep getting this error???
1 Comment
Gerardo Melgoza
on 28 May 2020
x=-3:0.1:3;
y=-3:0.1:3;
w=0.1;
[X,Y]=meshgrid(x,y);
z=w(X.^2-Y.^2);
Array indices must be positive integers or logical values.
help :c
1 Comment
Walter Roberson
on 28 May 2020
MATLAB does not have implicit multiplication anywhere (not even in the symbolic engine.)
w(X.^2-Y.^2) is a request to index w at location computed by X.^2-Y.^2
Philip Lopez
on 29 May 2020
Edited: Walter Roberson
on 10 Sep 2020
V = [0.001:0.001:0.6];
for i = 1:length(V)
I(i) = I_L - Is(exp((q*V(i))/(k*T)) - 1); Array indices must be positive integers or logical values.
end
1 Comment
Philip Lopez
on 29 May 2020
Nevermind, I figured it out. I forgot the multiplication symbol between Is and exp
Athanasios Karamanos
on 3 Jun 2020
clc; clear all; close all;
H = 100; u0 = 5; g = 9.81;
tfin = (2*H/g)^(1/2);
for t=0:0.05:tfin
x(t) = u0*t;
ye(t) = H-(1/2)*g*t*t;
ym(t) = H-(1/2)*(g/6)*t*t;
end
Array indices must be positive integers or logical values.
Error in Karamanos_6 (line 7)
x(t) = u0*t;
Someone help the noob plz.
1 Comment
Walter Roberson
on 3 Jun 2020
When you see something of the form
variable(NAME) = expression involving NAME
then you need to ask yourself whether you are looking at an array assignment, or if you are instead looking at a formula.
For example,
y(t) = g/2*t^2
is probably a formula, with the intention being to relate how to calculate y given indefinite t values.
On the other hand,
y(K) = g/2*t(K).^2
is probably an array assignment, telling you how to calculate the K'th array element given the index K and a way of mapping array indices to particular time values, t(K) [which would typpically be an array of t values, but could also be a function.)
The only part of MATLAB that deals with formulas in the form
variable(NAME) = expression involving NAME
is the Symbolic Mathematics Toolbox, and in order for it to work, NAME must indicate a symbolic variable, such as
syms t
y(t) = g/2*t^2
which creates a Symbolic Function named y that relates t values to y values.
Most of MATLAB uses a different syntax for formulas, that would look more like
y = @(t) g/2*t.^2
This is creating an "anonymous function" and storing it in y, and when y is invoked on a vector of values, the output would be calculated values. For example,
Y = y(0:0.05:tfin)
But what you should be doing is learning this general pattern:
t_vals = 0:0.05:tfin; %create an array of all of the values to be processed
num_t = numel(t_vals); %how many are there?
x = zeros(size(t_vals)); %create output variables the same size
ye = zeros(size(t_vals));
ym = zeros(size(t_vals));
for t_idx = 1 : num_t %loop over NUMBER of elements, not over the values
t = t_vals(t_idx); %pull out the "current" value from the list of values
x(t_idx) = u0*t; %calculate and store according to current index
ye(t_idx) = H-(1/2)*g*t*t;
ym(t_idx) = H-(1/2)*(g/6)*t*t;
end
If you use this pattern of storing the list of values to be operated on, and looping by index, then the list of values does not have to be in any particular order and does not have to be unique. You are operating based on relative position, not trying to figure out where to store the output based upon the "current" value:
x_vals = randn(1, 50); %does not need to be in order or regularly spaced
num_x = numel(x_vals);
y = zeros(size(x_vals));
mean_x = mean(x_vals(:));
std_x = std(x_vals(:));
for x_idx = 1 : num_x
x = x_vals(x_idx);
y(x_idx) = (x - mean_x)./std_x;
end
This is a general pattern that can be used even when the values to be operated on are in a "nice" order. It can be optimized in some special cases, such as
y = zeros(1,11);
for t = 0 : 10;
y(t+1) = g/2*t.^2; %the +1 compensates for the fact that t starts at 0
end
tutkuçiçek duruer
on 11 Jun 2020
x_1=0.1
x_2=0.5
x_3=5.0
z(x_1)=30.20
z(x_2)=7
z(x_3)=10.6
x4=x_2-0.5*((((x_2-x_1)^2)*(z(x_2)-z(x_3)))-((x_2-x_3^2)*(z(x_2)-z(x_1)))/(((x_2-x_1))*(z(x_2)-z(x_3)))-((x_2-x_3)*(z(x_2)-z(x_1))))
Array indices must be positive integers or logical values.
1 Comment
Walter Roberson
on 11 Jun 2020
your x_1 is 0.1. You try to assign to z(x_1) which is an attempt to index z at offset 0.1 which is not legal.
Please read https://www.mathworks.com/matlabcentral/answers/395816-why-do-i-get-array-indices-must-be-positive-integers-or-logical-values-error-when-using-angle-fu#comment_882605
Vikram Shah
on 15 Jun 2020
m = 10;
v = zeros(13,1);
k1 = 1;
f = 1;
R = 0 ;
g = 10;
for s=1:1:12
t = (-v(s) + sqrt(v(s).^2 + 2.*s((f(s)./m(s) - g - k1.*v(s)./m(s))))) ./ (f(s)./m(s) - g - k1.*v(s)./m(s));
R = R + t;
end
Not sure why I am getting the error 'Array indices must be positive integers or logical values.'
0 Comments
Zafoon Bibi
on 26 Jul 2020
v=0.0:0.25:0.75;
cosx=zeros(size(v));
N=10;
range=0:N;
ints=2*range;
for n=range
cosx=cosx +(-1)^n*v.^ints(n)...
/factorial(ints(n));
end
Why this code give error that array indices must be positive integers or logical values
0 Comments
DSai Babu
on 14 Aug 2020
Edited: Walter Roberson
on 14 Aug 2020
clc;
ybus=[-i*7 i*3 i*4; i*3 -i*8 i*5; i*4 i*5 -i*9];
pg=[0 0.25 0];
qg=[0 0.15 0 ];
pd=[0 0.5 0.6 ];
qd=[0 0.25 0.3];
slackbus=1;
loadbus=[2,3];
n=length(ybus);
v=[1.02 1 1];
1 Comment
Walter Roberson
on 14 Aug 2020
? That does not appear to be an Answer to the Question that was asked?
If you are having the array-indices error with that code, then you may have accidentally created a variable named length in which case length(ybus) would be interpreted as a request to index the variable.
ekta yadav
on 10 Sep 2020
I am getting the same error in line one
%%
% P-QRS-t
R_A(i) = zeros(1, 12);
R_t(i) = zeros(1, 12);
Q_A(i) = zeros(1, 12);
Q_t(i) = zeros(1, 12);
S_A(i) = zeros(1, 12);
S_t(i) = zeros(1, 12);
P_A(i) = zeros(1, 12);
P_t(i) = zeros(1, 12);
T_A(i) = zeros(1, 12);
T_t(i) = zeros(1, 12);
for i=1:length(left)
[R_A(i), R_t(i)]=max(sigL(left(i):raight(i)));
R_t(i)=R_t(i)-1+left(i); %add offset
[Q_A(i), Q_t(i)]=min(sigL(left(i):R_t(i)));
Q_t(i)=Q_t(i)-1+left(i);
[S_A(i), S_t(i)]=min(sigL(left(i):raight(i)));
S_t(i)=S_t(i)-1+left(i);
[P_A(i), P_t(i)]=max(sigL(left(i):Q_t(i)));
P_t(i)=P_t(i)-1+left(i);
[T_A(i), T_t(i)]=max(sigL(S_t(i):raight(i)));
T_t(i)=T_t(i)-1+left(i)+47;
end
where - left=1X12 double;
ERROR-Array indices must be positive integers or logical values.
2 Comments
Jordan L'Esperance
on 15 Sep 2020
%LEsperance, J. 9/15/2020
%EAS230
%Indicated Air Speed to True Air Speed conversion
%a0, b0, and P0 are given constants
%P0 is standard pressure at sea level
%IAS, T, and P are variables
a0=87, b0=1479, P0=101325; %constants
IAS=275, T=248.526, P=46563.25; %variables
TAS = a0*sqrt(T*([P0/P((IAS^2/b0^2+1)^7/2-1)+1]^2/7-1)); %True Air Speed
Yeah so this is my script and I'm getting the same error message to no avail. Any help would be greatly appreciated!
Pranoti Devarde
on 17 Sep 2020
Edited: Walter Roberson
on 18 Sep 2020
k=0.2
p2=(V^2*sin(del)) /X(1-k);
q2=[2*V^2*k(1-cos(del)) ]/X(1-k)^2;
Plot(del,p2, q2);
I got error Array indices must be positive integers or logical values.what i do in that case?
1 Comment
Walter Roberson
on 18 Sep 2020
MATLAB has no implied multiplication. k(1-cos(del)) is requesting indexing of k.
Dan Li
on 23 Sep 2020
Anyone can help with my code below? Geting the error : Array indices must be positive integers or logical values.
Not sure why. Please help.
% specify values for parameters
t1 = .2;
t2 = .5;
h0 = 1;
a = .5;
b = .3;
uo = 1;
% define y function for all three cases
t = linspace(0,50,10000);
y_range1 = 0;
y_range2 = (uo*h0/(b^3*(t2-t1))).*(exp(b*t1-b*t).*(-t1*b + b*t +2) - t1*b +t*b -2);
y_range3 = (uo*h0/(a-b)^2).*(exp(b(t1+t2-t)-t1*a).* (-(a-b).*(t1+t2-t) + exp((a-b).*(t1+t2-t))-1))...
+ (uo*h0/(b^3*(t2-t1))).*(exp(b*t1-b*t).*(-exp(b*t2).*(b*(t1(b*t2 -1) + t2*(b(t2-t)-2)+t)+2)-t1*b +b*t +2));
%plot the output
y = y_range1.*(t < t1) + y_range2.*((t>=t1)&(t<=t2)) + y_range3.*(t>t2);
plot(t,y)
2 Comments
Swarnadeep Bagchi
on 29 Sep 2020
Make sure that in Matlab2019 which I am using, using min() and max() method requires clear min and clear max commands as shown below
y = [2 5 17 9 20] ; % Any vector
clear min
min_val = min(y) ;
clear max
max_val = max(y) ;
timeA = 7;
timeB = 8;
snip = (timeA*data.Fs):(timeB*data.Fs) ;
2 Comments
Swarnadeep Bagchi
on 29 Sep 2020
Even when variables names with min and max is not present in the script, still we need to use in Matlab2019, becoz I was getting the same error.
Marco Serrano
on 30 Sep 2020
Edited: Walter Roberson
on 20 Oct 2020
Hi, can anybody help me fix the code below? I'm supposed to plot the kinematic coordiantes with respect to time but I get the error: Array indices must be positive integers or logical values. Thanks in advance.
*function file
function [x,y] = ProjectileMotion(x0,y0,v0,theta0,t)
%ProjectileMotion computes the kinematic coordinates[x,y] of the projectile
% Uses the initial values of position, velocity, angle, and time of
% flight to calculate kinematic coordinates
x = x0+v0*cosd(theta0)*t;
y = y0+v0*sind(theta0)*t-(0.5)*(9.8)*t^2;
end
*script file
x0=input('What is the initial position with respect to x in [m]?:');
y0=input('What is the initial position with respect to y in [m]?:');
v0=input('What is the initial velocity in [m/s]?:');
theta0=input('What is the angle of release with respect to x in [deg]?:');
t=input('What is the time of flight in [s]?:');
[x,y]=ProjectileMotion(x0,y0,v0,theta0,t)
flight_t=[0:t];
x(flight_t)=x0+v0*cosd(theta0)*(flight_t);
y(flight_t)=y0+v0*sind(theta0)*(flight_t)-(0.5)*(9.8)*(flight_t).^2;
plot(flight_t,x(flight_t),flight_t,y(flight_t))
0 Comments
Clint Han
on 20 Oct 2020
I too am getting issues with this error. From scouring my code, I don't see any conflcting variables
maxT=zeros(25,25);
for i=1:length(t4)
for j=1:length(t4)
if t4(i,j)>0.92 & t4(i,j)<1
maxT(triu(i,j))=t4(triu(i,j)) %<- this is where the error line is%
end
end
end
1 Comment
Walter Roberson
on 20 Oct 2020
Your i is a scalar.
triu(A) takes a matrix and sets the lower left triangle to 0. triu(A,K) sets the elements below the K'th diagonal to 0.
You are applying triu(i,j) which is passing the scalar i into triu() as the matrix. When you have a scalar, the only diagonal that has non-zero elements is the 0'th diagonal. Your j is never 0, so triu(i,j) is always going to be 0. You then use that 0 to index t4 and maxT.
Samuel Gabriel
on 29 Oct 2020
clear;clc
x = 3.8 : 0.9 : 13.7;
for n = 1 : length(x)
y(x) = -10 * (-0.2 * x(n) ^ 2 - 5 * x(n))
end
y(x)
Error says
Array indices must be positive integers or logical values.
Error in (line 4)
y(x) = -10 * (-0.2 * x(n) ^ 2 - 5 * x(n))
I do not understand. I thought decimals could be in vectors. Am I missing a command?
2 Comments
Klara Carty
on 17 Nov 2020
I seem to be getting the exact same error when i do the loop... am i missing something?
x=[.1:.1:2];
y=1.\x + sqrt(x).*exp(x);
a=y(1);
b=y(end);
n=numel(x);
h=(b-a)/n;
sum=0;
for i=1:1:n-1
sum=sum+y(a+i*h)
end
3 Comments
Steven Lord
on 18 Nov 2020
I can evaluate a function at x = 1.5 (for example) but I can't ask for element 1.5 of an array.
f = @(x) 1./x + sqrt(x).*exp(x);
f(1.5) % Evaluate a function
x = 1:10;
y = f(x); % Create a data array
y(1.5) % Indexing into the array throws an error
Mellamitha Andreas
on 13 Dec 2020
i got this problem too, i supposed to plot IDFT. Thank you!!!
>> N=length(y);
m=zeros(1,N);
for n=0:N-1
for k=0:N-1
m(n+1)=m(n+1)+((1/N)*y((k+1)*exp((1i)*2*pi*k*n)/N));
end
end
Array indices must be positive integers or logical values.
0 Comments
Ruzimatjon Sultonov
on 20 Jan 2021
Edited: Walter Roberson
on 20 Jan 2021
xd=0.9;
xad=0.8;
x5=0.5;
xq=0.64;
xaq=0.54;
xfd=1.68;
xfq=1.03;
xkd=0.33;
xkq=0.66;
rfd=0.282;
rfq=0.0564;
rkd=0.24;
rkq=0.17;
Dd=xd*xfd+xkd+2*(xkd^3)-xad*(xd+xfd+xkd);
Dq=xq*xfq*xkq+2*(xaq^3)-xaq*(xq+xfq+xkq);
Ad1=((xad^2)-xad*xkd)/Dd;
Ad2=((xad^2)-xd*xkd)/Dd;
Ad3=((xad^2)-xd*xad)/Dd;
Ad4=((xad^2)-xad*xad)/Dd;
Ad5=((xad^2)-xd*xad)/Dd;
Ad6=(xd*xfd-(xad^2))/Dd;
Aq1=((xaq^2)-xaq*xkq)/Dq;
Aq2=((xaq^2)-xq*xkq)/Dq;
Aq3=((xaq^2)-xq*xaq)/Dq;
Aq4=((xaq^2)-xaq*xfq)/Dq;
Aq5=((xaq^2)-xq*xaq)/Dq;
Aq6=(xq*xfq-(xaq^2))/Dq;
U=1;
Ufd=34;
Ufq=0;
theta=2.4*pi/180;
cos(theta);
t=(0, 1);
t=(0, 1);
↑
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise,
check for mismatched delimiters.
t=[0 10];
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*ifd(t))-Aq3*rkd*ikd(t);
Unrecognized function or variable 'ifd'.
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*"ifd(t)")-Aq3*rkd*ikd"(t)";
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*"ifd(t)")-Aq3*rkd*ikd"(t)";
↑
Error: Invalid use of operator.
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*"ifd*(t)")-Aq3*rkd*ikd*"(t)";
Operator '*' is not supported for operands of type 'string'.
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*"ifd"*"(t)")-Aq3*rkd*ikd*"(t)";
Operator '*' is not supported for operands of type 'string'.
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*)-Aq3*rkd*ikd(t);
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*)-Aq3*rkd*ikd(t);
1 Comment
Walter Roberson
on 20 Jan 2021
syms ifd(t) ikd(t)
Ifd=Ad1*U*cos(theta)-Ad2*(Ufd-rfd*ifd*(t))-Aq3*rkd*ikd(t);
Joseph Capp
on 1 Feb 2021
Array indices must be positive integers or logical values.
Error in p2 (line 18)
f(re)=3*(re)^4-6*(re)-6;
how do I fix this error
error=0.1;
ru=2;
rl=1;
re=(rl+ru)/2;
while abs(ru-rl)>error
f(rl)=3*(rl)^4-6*(rl)-6;
f(ru)=3*(ru)^4-6*(ru)-6;
%disp(f(rl));
%disp(f(ru));
f(re)=3*(re)^4-6*(re)-6;
if (f(rl)*f(re))<0
ru=re;
else
rl=re;
end %if
re=(rl+ru)/2;
end %while
root=re;
disp(root);
2 Comments
Ibrahim Usman
on 23 Feb 2021
Hi, I am getting the same error for my code. I tried putting the v function outside the for loop to no avail. My call to the function is
m = Amountcomp(t,v,m0)
function [m] = Amountcomp(t,v,m0)
% v = 0.69*exp(-2.3*t);
for i= 0:0.6250:2.5
v(i) = 0.69*exp(-2.3*t(i));
m = m0 + integral(v(i),0,t);
end
t_r = round(t,2);
m_r = round(m,5);
formatSpec1 = 't_r is %4.2f hours' ;
fprintf(formatSpec1,t_r)
formatSpec2 = 'm_r is %4.5f moles' ;
fprintf(formatSpec2,m_r)
plot(t_r,m_r, '--g')
end
6 Comments
Walter Roberson
on 23 Feb 2021
syms t v m
That overwrites all of v with a symbolic variable v.
m = m0 + integral(v(i),0,t);
That asks to apply integral to the symbolic variable indexed at i . i happens to start at 1, so that is v(1) which is valid for a symbolic variable. But if you got past that step then the next iteration you would be asking to index the symbolic variable at 2, but it is a symbolic scalar.
But you will not get past that step, because you cannot apply integral() to a symbolic variable. If you want to do symbolic integration you would have to use int() . But remember that at this point v is just a symbolic scalar, and integrating the symbolic variable with respect to the default variable (which would be itself in this case) would give you 1/2*v^2 always.
ZIYI WENG
on 4 Mar 2021
I also get the same error and I think my value of phi should probably be negative all the time.
Here is the code
n = 8;
wl = 1;
d = wl./2;
AF = zeros(1,100);
th= -60;
angle_rad = th.*pi./180;
phi= (2.*pi.*d .*sin(angle_rad))./wl;
for a = 1:n
phi(angle_rad)=AF;
AF= (a-1).*phi;
end
How can I solve the problem and save the numbers of phi into the AF?
Because I want to do the further calculation with the AF array.
2 Comments
Rahul Verma
on 3 May 2021
Edited: Walter Roberson
on 11 Sep 2021
t = 0:.1:pi * 10;
y = sin(t);
figure
plot(t,y);
xlabel ('Freq (Hz) ');
ylabel ('Volt ( V ) ');
title (' Coil Input Voltage(Sine wave)');
vo = 1;
c = 45e-6;
r = 1800;
f = 50;
tf = 70e-3;
w = 2 * pi * f;
t = 0:0.05e-3:tf;
n = length (t);
state = 'on';
fori = 1:n
vs(i) = vo * sin(w * t(i));
in this code getting
"Array indices must be positive integers or logical values" this error
1 Comment
Walter Roberson
on 3 May 2021
fori = 1:n
That is not a for loop. That is an assignment to a variable whose name is f followed by o followed by r followed by i. Spaces are important.
Sudha R Jogin
on 26 Jul 2021
close all;
workspace;
vid= VideoReader('sony.mp4');
F= vid.NumFrames;
sum=0;
for i=1:F
video(i) = read (vid,i);
[rows , columns ]= size (video(i));
noisyvideo(i)= imnoise(video(i),"gaussian",0,0.02);
squareErrorFrame(i) = (double(noisyvideo(i))-double(video(i))).^2;
MSE = sum(sum (squareErrorFrame(i)))/(rows*columns);
sum= sum + MSE;
end
display(sum);
Index exceeds the number of array elements (1).
Error in forloopTrail (line 11)
MSE = sum(sum (squareErrorFrame(i)))/(rows*columns);
can someone help me in solving this please
0 Comments
Ar Chloch
on 10 Sep 2021
I get the same error with my code
hx = sigmoid(theta' .* X);
J = -1/m .* sum(y .* log(hx + (1 - y) .* log(1) - hx));
for i = 0:m
grad(i + 1) = 1 / m .* sum((hx - y) .* X(:,i));
end
Can anyone help me
2 Comments
Walter Roberson
on 11 Sep 2021
for i = 0:m
i starts at 0
grad(i + 1) = 1 / m .* sum((hx - y) .* X(:,i));
i is used as an index into X. But the first i is 0, so that starts trying to access X(:,0) which is not valid in MATLAB.
In some cases you want to access the "previous" value. For example for a filter of the form
there is a temptation to write code along the lines of

for n = 1 : 10
y(n) = 3*x(n) - 2 * y(n-1);
end
However this does not work in practice, because n starts at 1 and y[1-1] = y[0] is not defined. You end up having to write something closer to
y(1) = initial value
yoff = 1;