フィルターのクリア

array indices must be positive intergers or logical values - not sure why i am getting this error message. same code worked for a friend of mine.

2 ビュー (過去 30 日間)
clf reset
x=0:0.1:1;
y=[0.9,6.9,31.6,21.5,34.4,68.2,66.4,114.9,155.9,199.3,209.6];
p1=polyfit(x,y,1);
% the code below calculates the r2 values. You can just copy and paste this
% code after your polyfit to find r2 (as long as you use x,y and p1)
% The polyval function evaluates the equation of the fit at the values of x.
yhat_1=polyval(p1,x);
% coefficient of determination
r2=(sum((y-mean(y)).^2)-sum((y-yhat_1).^2))/sum((y-mean(y)).^2);
fprintf('fit y=%.2fx%+.2f \nR2=%.2f\n', p1(1), p1(2),r2);
  4 件のコメント
John McCann
John McCann 2021 年 2 月 3 日
defintely the same code, even when i run it in a brand new script i still get the error
John McCann
John McCann 2021 年 2 月 3 日
somehow it works now, not sure what the problem was but restarting matlab a few times worked.
thanks for you help

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

採用された回答

KSSV
KSSV 2021 年 2 月 3 日
Read about clear. When you feel you are done with your previous code, then you can clear all your workspace variables and then run the fresh code. It looks there might be a variable conflict earlier when there was error.

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 2 月 3 日
My guess is that earlier in that code you created variables named sum and/or mean. Attempts to call sum and/or mean while variables named sum and/or mean exist generally will not work, they will be interpreted as attempts to index into the variable.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by