解釋清楚你在哪裏執行上面的代碼? 如果您在命令提示符中執行,請勿使用函數。代碼喜歡這個
a=[ 1 2 3; 4 5 6; 7 8 9];
%takes a matrix as a input
display(' data matrix');
disp(a);
disp('dimension of the matrix');
[m n]= size(a); %calculate the dimension of data matrix
s = mean(a);
,或者如果您不使用命令提示符,然後把值另一個函數「一」,並呼籲從創建新的功能try_1功能。這樣的代碼
function parent()
a=[ 1 2 3; 4 5 6; 7 8 9];
s= try_1(a)
end
function [s]= try_1(a)
%takes a matrix as a input
display(' data matrix');
disp(a);
disp('dimension of the matrix');
[m n]= size(a); %calculate the dimension of data matrix
s = mean(a);
end
或分配則try_1函數內的值。這樣的代碼
function [s]= try_1(a)
a=[ 1 2 3; 4 5 6; 7 8 9];
%takes a matrix as a input
display(' data matrix');
disp(a);
disp('dimension of the matrix');
[m n]= size(a); %calculate the dimension of data matrix
s = mean(a);
end
你有什麼錯誤? – Mehraban 2015-04-06 06:21:51
錯誤是「在此上下文中不允許使用函數定義」。 – 2015-04-06 06:22:50
[This](http://stackoverflow.com/questions/5363397/in-matlab-can-i-have-a-script-and-a-function-definition-in-the-same-file)可能有幫助 – 2015-04-06 06:26:05