least-square problem and calling of that function

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by