Why do I get "Array indices must be positive integers or logical values"

1 回表示 (過去 30 日間)
maria serey-roman
maria serey-roman 2019 年 4 月 25 日
編集済み: Walter Roberson 2019 年 4 月 25 日
Very new at matlab, i'm trying to fit linear function to a set of values. I keep getting "Array indices must be positive integers or logical values"
Please advise.
x = [62857 63320 63202 64754 64109 64298]'; % Population
y = [16366669 16864938 17087795 17224537 17301839 17095073]'; % Ridership
n = numel(y); % number of points being fitted
plot(x,y, '+')
A = [ones(6,1), x]; % design matrix
m = size(A,2); % parameters that we want fitted
b = A \ y;
f(x) = b(1) + b(2) *x;
xi = linspace(0,100);
fxi = f(xi);
plot(x, y, '+', xi, fxi)

採用された回答

Mauro Fusco
Mauro Fusco 2019 年 4 月 25 日
Arrays in Matlab are indexed starting from 1. Just replace xi = linspace(0,100); with
xi = linspace(1,100);
and it will not give error.
  2 件のコメント
maria serey-roman
maria serey-roman 2019 年 4 月 25 日
Thank you, that worked!
Walter Roberson
Walter Roberson 2019 年 4 月 25 日
This deals with the problem accidentally, and does not solve the real issue.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 4 月 25 日
編集済み: Walter Roberson 2019 年 4 月 25 日
Also, your x range is about 60000, but the range over which you plot the projected line is 0 to 100.
plot(x, y, '+', x, polyval(flipud(b), x))

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by