For loop error in Matlab

matlab machine-learning linear-regression

66 просмотра

1 ответ

3 Репутация автора

I am trying to run a for loop as part of a hypothesis function for a linear regression in Matlab, however am struggling to get it to run. Below is the function:

function hypothesis = calculate_hypothesis(X, theta, training_example)

%CALCULATE_HYPOTHESIS This calculates the hypothesis for a given X,
%theta and specified training example

hypothesis = 0; 
for training_example = 1:size(X,1)
    hypothesis = theta'.*X(training_example,:) + hypothesis


X is a 47x3 matrix (3 variables in the linear regression equation, with 47 examples), and theta is a 1x3 matrix representing the weights for the variables. Note that the objective here is to 'learn' the correct weights for the equation from the 47 examples using gradient descent, but I'm comfortable with the methodology for this. What I'm having trouble with is getting the matlab code to run for the hypothesis equation. The error I get is

Not enough input arguments. Error in calculate_hypothesis (line 5) for training_example = 1:size(X,1)

Am new to Matlab, so any help would be much appreciated.

Автор: Vishal Источник Размещён: 08.11.2017 11:20

Ответы (1)

-1 плюса

350 Репутация автора

You don't need to pass


as a parameter to your function

calculate_hypothesis(X, theta, training_example)

Because whatever value you pass will be overwritten by the for loop, which applies new values to training_example with every iteration. I guess you are only calling calculate_hypothesis() with X and theta and thus get the error "not enough input arguments".

Try changing

calculate_hypothesis(X, theta, training_example)


calculate_hypothesis(X, theta)
Автор: Marcus Размещён: 08.11.2017 11:34
Вопросы из категории :