Chapter 15: Exercises
What is "H1 help" in a MATLAB function? What is the advantage if we provide H1 help in a MATLAB function?
What are returned by "nargin" and "nargout" in a MATLAB function? What advantages can these two commands provide?
What are the values of "varargin" and "varargout" in a MATLAB function? What advantages can these two variables provide?
What is the major purpose of using "mfilename" in a MATLAB function?
What is the major purpose of using "inputname" in a MATLAB function?
Does the MATLAB function employ "call by value" or "call by reference" in passing the input arguments?
How does "private" directory work in a MATLAB function?
When you invoke a MATLAB function, what is the search sequence used by MATLAB?
What are the advantages and disadvantages of using global variables in a MATLAB function?
Explain how to share a global variable X within func01.m and func02.m.
關於函數,下列何者正確?
實際運作上,未被修改的輸入變數依舊採用 "Call by Reference" 以降低記憶體使用量。
包含在M檔案中的函數可以不止一個,且皆能被檔案外的的其他函數所呼叫。
函數名稱可以和檔案不相同,呼叫時以檔案名稱為主。
可以接受變數輸入,但以兩個為上限。
Ans: a, c.
請簡述 p-code 功能,並說明如何將 func.m 轉化成 p-code 形式。
Ans: 我們可以使用 p-code 來保護程式碼的內容。一般的 M 檔案可以看的到原始程式碼,若想讓別人使用自己的程式碼,又不想被看到程式碼的內容,可以使用 pcode 指令將 m 檔案轉成 p-code 檔案。
例如,下列命令可將 func.m 轉換成 p-code:
pcode func.m
dir *.p
藉由將 m 檔案轉換成 P-Code(Pseudo code),可保護自己的程式碼不讓別人看到,且沒有任何方法可以將P-Code轉成.m檔案。請問還有其他方法可以保護自己的 MATLAB 程式碼嗎?
Ans: 有,我們可以把 m 檔利用 MATLAB compiler 編譯成 binary 格式(獨立的程式,可以直接執行),就可以隱藏原始碼的內容。
請比較 script m-file 跟 function m-file 的不同。
Ans:
答:script 為包含一系列 MATLAB 指令的 m-file,當執行 script 檔,script 中所建立的變數會被建立到目前的 workspace。function 有輸入輸出變數,script 則沒有,函數中的變數除了有特別宣告,不然都是 local variable,這些變數屬於該 function 的 workspace,function 執行結束就會被清除掉。
Projection :
撰寫一函式 projection01.m,其輸出入的格式如下:
out = projection01(x, u)
其中 x 和 u 是維度相同的向量,out 則是 x 在 u 方向的投影量的平方值。
一元二次方程式的根 :
撰寫一函式 quadzero01.m,其輸出入的格式如下:
root = quadzero01(coef)
其中 coef 是一個 3x1 的向量,代表一個一元二次方程式的係數(降冪排列),而 root 則是此方程式的根所形成的向量。若 coef(1) 不是零,則 roots 的長度為二。若 coef(1) 是零,則可能有一解(root 的長度為一)或無解(root為空矩陣)。(請注意:你的程式碼必須套用標準一元二次方程式 a*x2 +b*x+c=0 的公式來解此題,而不可以直接使用 roots 指令來解此題。)
由基本代數可知,一個實係數的一元三次方程式,必有一個實數解。請寫一函式 trizero01.m,其輸出入的格式如下:
root = trizero01(coef)
其中 coef 是是一個 4x1 的向量,代表一個一元三次方程式的係數(降冪排列),而 root 則是一個 3x1 的向量,代表此方程式的三個根。(為簡化題目,我們假設 coef(1) 不等於零,因此一定會有三個根。)請參照下列步驟來撰寫你的程式:
使用 fzero 指令來找出此方程式的實數根 x1。
使用 deconv 指令來算出將此多項式除以 (x-x1) 後得到的商式。
使用前一個習題的函式來找出此商式(已是一元二次方程式)的根。
寫一個 MATLAB 遞迴函式 combine.m,其功能是可對輸入字串進行組合。例如,combine('abcd', 2) 所傳回的字串矩陣是
ab
ac
ad
bc
bd
cd
此字串矩陣的每一列代表從輸入字串任取兩個字元時,所有可能的組合。(你的排列順序不見得要和上例完全一樣。)為求簡單起見,我們可假設輸入字串並沒有重複的字元。
15-M檔案/combine.m function out = combine(obj, n)
% out = combine(obj, n) returns combinations of obj with n distinct
% elements.
% For instance: combine([1 2 3 4 5], 2) or combine('abcde', 3).
% Roger Jang, Sept-21-1996
%if n==1|n==length(obj),
% out = obj(:);
% return;
%end
if n==1,
out = obj(:);
return;
end
if n==length(obj),
out = obj(:)';
return;
end
out = [];
for i = 1:length(obj)-1,
first = obj(i);
tail = obj(i+1:length(obj));
tail_combinat = combine(tail, n-1);
loop_out = [first*ones(size(tail_combinat,1), 1), tail_combinat];
out = [out; loop_out];
end
請重複上一題,但假設輸入字串可能含有重複的字元。
寫一個 MATLAB 遞迴函式 permute2.m,其功能是可對輸入字串進行排列。例如,permute2('abc', 2)所傳回的字串矩陣是
ab
ac
ba
bc
ca
cb
此字串矩陣的每一列代表輸入字串所有可能的排列。(你的排列順序不見得要和上例完全一樣。)為求簡單起見,我們可假設輸入字串並沒有重複的字元。(此函數名稱訂為 permute2,是因為 MATLAB 本身已經有一個名稱是 permute 的內建函數。)
15-M檔案/permute2.m function out = permute2(obj, n)
% out = permut(obj, n) returns permutation of obj with n elements.
% All elements of obj is assumed to be distinct.
% If n is not given, length(obj) is used instead.
% For instance: permute2([2 3 4 5], 2) or permute2('abcde').
% Roger Jang, Sept-24-1996
if nargin == 1,
n = length(obj);
end
if length(obj) == 1,
out = obj;
return
elseif n == 1,
out = obj(:);
return;
end
out = [];
for i = 1:length(obj),
first = obj(i);
remainder = obj;
remainder(i) = [];
remainder_permute = permute2(remainder, n-1);
loop_out = [first*ones(size(remainder_permute,1),1), remainder_permute];
out = [out; loop_out];
end
請重複上一題,但假設輸入字串可能含有重複的字元。
Suppose the function myFun.m is defined as:
function total = myFun(varargin)
total=0;
for i=1:length(varargin)
disp(sprintf('Input %d is "%s".', i, inputname(i)));
total=total+varargin{i};
end
Then what will be printed out if we invoke myFun.m as follows:
x=5;
y=[1 2];
z=myFun(x, y, 4);
MATLAB 中的 union 指令只能對兩個輸入陣列進行聯集運算,請寫個新的函數,可以對任意個數的輸入陣列進行聯集運算。
MATLAB程式設計:入門篇