least-square problem and calling of that function

1 回表示 (過去 30 日間)
Erik
Erik 2014 年 4 月 10 日
回答済み: Star Strider 2014 年 4 月 10 日
Hello!
I seem to have a problem with least-square method. I would estimate that the error is simple but I must admit I am code blind at this moment. The file is displayed below and the error directly after. I have marked the rows in the .m-file which is displayed in the error message with >>>.
function[A Deltapar] = MKA(x,y,deltay,n) A = []; Deltapar = []; X=[];
%-----------------------------------------------------------------------------------------------% checking if the input for the matrix size is correct >>>if(size(x)(2)~=1) x=x'; end if(size(y)(2)~=1) y=y'; end if(size(deltay)(2)~=1) deltay=deltay'; end %----------------------------------------------------------------------------------------------- % checking if the matrix x,y, and deltay has the correct size and after that, execute least square method.
if(size(x)(2)==1 && size(y)(2) ==1 && size(deltay)(2)==1)
%----------------------------------------------------------------------------------------------- % Conducting weighted least squares fit on the matrices x and y have the same format. % If not, a message is returned back an x and y are not of the same length. if(length(x)==length(y)) for i=0:n X=[X x.^i]; end sigma=deltay; vminus1 = diag(1./sigma.^2); A = inv(X' * vminus1 * X) * (X' * vminus1 * y); DeltaA = inv(X' * vminus1 * X); Deltapar = sqrt(diag(DeltaA)); else disp('x,y does not have the same length'); end %----------------------------------------------------------------------------------------------- % prints out "you have not written the input on correct form"
else disp('you have not written the input on correct form'); end
??? Error: File: MKA.m Line: 17 Column: 4 ()-indexing must appear last in an index expression.
except this problem I seem to have difficulties with calling this file from another .m-file having code shown in the error message.
the error message is as follow: Error in ==> absnoll2 at 80 [a]= MKA(x,y,deltay,1);
Thanks in advance! sorry for the typo..
Friendly Regards, Erik.

採用された回答

Star Strider
Star Strider 2014 年 4 月 10 日
If you are testing for the column size of x, change the call to size to:
size(x,2)
(and for other arrays) in your if statements.
There may be other problems, but that should eliminate the error you are getting.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by