Index indices must be natural integers or logical targets

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
%   theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by 
%   taking num_iters gradient steps with learning rate alpha

% Initialize some useful values  
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
%               theta. 
%
% Hint: While debugging, it can be useful to print out the values
%       of the cost function (computeCost) and gradient here.
%

    hypothesis = x*theta;
    theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);
    theta_1 = theta(2) - alpha(1/m)*sum((hypothesis-y)*x);
    theta(1) = theta_0;
    theta(2) = theta_1;








% ============================================================

% Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);

end

end

      

I keep getting this error

error: gradientDescent: subscript indices must be either positive integers less than 2^31 or logicals

      

on this line right between the first theta and =

theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);

      

I'm very new to octaves, so please calm down and thanks in advance. This is from the Computer Learning Cursor course from week 2

+3


source to share


1 answer


99% make sure your error is listed on the line given in topsig where you have alpha(1/m)

it will help if you provided an example of the inputs to your function and what you were hoping to see as output, but I assume from your comment

% taking num_iters gradient steps with learning rate alpha

which alpha

is a constant, not a function. as such, you have a string alpha(1/m)

without any operator in between. octave sees this when indexed alpha

with a value 1/m

.

ie if you had an array



x = [3 4 5]
x*(2) = [6 8 10]  %% two times each element in the array
x(2) = [4]  %% second element in the array

      

what you did doesn't seem to make sense since "m = length (y)" which outputs a scalar, so

x = [3 4 5]; m = 3;
x*(1/m) = x*(1/3) = [1 1.3333 1.6666]  %% element / 3
x(1/m) = ___error___  %% the 1/3 element in the array makes no sense

      

note that for certain errors, it is always said that the location of the error is relative to the assignment operator (equal sign at the beginning of the line). if it points there, you usually have to look elsewhere in the line for the actual error. here he yelled at you for trying to apply a non-integer index(1/m)

+2


source







All Articles