Search code examples
matlabmatrixdeep-learningstatisticsleast-squares

Error using * Inner matrix dimensions must agree in using Least Squares - how to make the regressor array for multiple independent variables


I am trying to learn how to code for linear regression where the data statistics_data represents the yeast growth year in first column, the value of a chemical component in the second column and the value of the population in third column. Once theta is calculated using least squares formulation, I want to predict the value of the population using: pred_year = 2020; pred_year_val = [1 2020]; which is giving this error:

Error using  * 
Inner matrix dimensions must agree.

Error in main_normal_equation (line 44)
pred_value = pred_year_val * theta;

Below is the code:

    statistics_data = [2007, 9182927, 2;
2008,3,9256347;
2009,3.5,9340682;
2010,4,9415570;
2011,5,9482855;
2012,4.8,9555893;
2013,4.9,9644864;
2014,5,9747355;
2015,5,9851017;
2016,5,9995153;
2017,5,10120242;];


% Convert to independent variable matrix and response
X = (statistics_data(:,1:2));
y = (statistics_data(:,3));

% Convert matrix values to double
X = double(X);
y = double(y);

hold on;
% Set the x-axis label
xlabel('Year'); 
% Set the y-axis label
ylabel('Population'); 

% Plot population data
plot(X, y, 'rx', 'MarkerSize', 10);

m = length(y);
% Add ones column
X = [ones(m, 1) X];

%  Normal Equation
theta = (pinv(X'*X))*X'*y

% Predict population for 2020
pred_year = 2020;
pred_year_val = [1 2020];

% Calculate predicted value
pred_value = pred_year_val * theta;

% Plot linear regression line
plot(X(:,2), X*theta, '-')

   fprintf('Predicted population in 2020 is %d people\n ', int64(pred_value));

Solution

  • In matlab when you use the * operator, you are referencing a matrix multiply. Matrix multiplication has strict rules about the dimensions of the multiplied matrices.

    Inspecting your code, it does not seem that your intent is to do a matrix multiply....

    You can multiply a scalar by a matrix using * and scale each value in the matrix accordingly.

    You can also vector multiply which is sometimes called element by element multiplication using the .* operator.

    To resolve your issue you must clarify whether you intended to do a matrix multiply, scalar multiplication, or a vector multiplication. Then you must properly set your operands and operator to reflect what it is you aim to achieve.

    It isn't clear to me exactly how the math in your code is supposed to be executed otherwise I could help show you where your operators and operands must be changed.

    You could start by reviewing the documentation here: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html