Chapter 12: Exercises

Part 1

  1. 請寫一段程式 buildCell01.m,建立下列 8×3 的異質陣列 A:
    張惠妹聽海1998
    周華健花心1992
    王傑一場遊戲一場夢1988
    孫燕姿遇見2003
    孫燕姿超快感2000
    F.I.R.Lydia2004
    蔡依林愛情36計2004
    王心凌明天見2004
  2. 在上題異質陣列中,每一橫列代表一筆資料,請寫一段程式 cellSort01.m,使要用不同的方法來排序:
    1. 請依歌星名字內碼來排序,產生新的異質陣列 B,並將結果印在螢幕。
    2. 請依年代來排序,產生新的異質陣列 C,並將結果印在螢幕。
    3. 請依歌名字數來排序,產生新的異質陣列 D,並將結果印在螢幕。(若字數相同,則用內碼來排序。)
  3. 請寫一段程式 cellStat01.m,於上題的異質陣列中,求出年代的最大值、最小值、平均值及中位數。
  4. 請寫一個函數 findCellStr.m,可以找出一個異值字串陣列(Cell string array)中的某一個特定字串。其用法如下:
    index = findCellStr(cellstr, str)
    其中 cellstr 是一個異值字串陣列,str 是一個字串,index 則是 str 在 cellstr 中出現的索引值。例如當 cellstr 是 {'ab', 'xyz', 'ab', 'cde'} ,str 是 'ab' 時,所傳回的 index 是 [1, 3]。
  5. 請寫一個函數 readFile.m,其用法如下:
    cellStr = readFile(fileName)
    其中,輸入引數 fileName 是一個字串,代表檔案名稱,輸出引數 cellStr 則是一個異值字串陣列,陣列的每一個元素都是一個字串,代表原檔案中的每一列資料。(不可使用 textread 或 fileread 指令來完成本題。)

    12-異質陣列/readFile.mfunction contents = readFile(fileName); %readFile: Read the content of a file and put it into a cell string % Roger Jang, 20010218 if nargin==0, selfdemo; return; end fid = fopen(fileName); if fid<0, error('Cannot open file!'); end lineNum = 1; while 1 line = fgetl(fid); if ~isstr(line), break, end contents{lineNum} = xlate(line); lineNum = lineNum+1; end fclose(fid); % Subfunction for self demo function selfdemo fileName = [mfilename, '.m']; contents = feval(mfilename, fileName); fprintf('The contents of "%s":\n', mfilename); for i=1:length(contents), fprintf('%s\n', contents{i}); end

  6. 請寫一個函數 split.m,其用法如下:
    tokenList = split(str, delimiter)
    其中,str 是一個輸入字串,delimiter 則是一個字元,此函數會將 str 依照 delimiter 的位置而拆開來,並將結果放入一個以異質字串陣列 tokenList。例如,當我們執行 split('This#is#a#test#string', '#'),產生的結果應該是{'This', 'is', 'a', 'test', 'string.'}。

    12-異質陣列/split.mfunction tokenList = split(str, delimiter) % split: Split a string based on a given delimiter % Usage: % tokenList = split(str, delimiter) % See also JOIN. % Roger Jang, 20010324 if nargin==0; selfdemo; return; end tokenList = {}; remain = str; i = 1; while ~isempty(remain), [token, remain] = strtok(remain, delimiter); tokenList{i} = token; i = i+1; end function selfdemo str='This-is-a-test'; tokenList=feval(mfilename, str, '-'); str fprintf('After running "tokenList=split(str, ''-'')":\n'); tokenList

Part 2

  1. Cell array addressing: Suppose B = {'James Bond', [1 2;3 4;5 6]; pi, 1:6}. What are the value returned by each of the following statements?
    1. B{1,1}(7:end)
    2. B{1,2}(3,1)
    3. sum(B{1,2}(3:end))
    4. cos(B{2,1})
    5. mean(B{2,2}(2:2:end))
  2. Cell array addressing: Let's execute the following statements within MATLAB: A = [7 5 6]; [myOutput{1:2}]=sort(A); plot(myOutput{:}, 'o-');
    1. What is the value of myOutput?
    2. What is the result of the plot command?
  3. Using 'num2cell' for a matrix: Suppose A = [1 2 3;4 5 6]. What are the value returned by the following statements?
    1. num2cell(A)
    2. num2cell(A, 1)
    3. num2cell(A, 2)
  4. Using 'num2cell' for a 3D array: Suppose matrix A is obtained via the following statements: A(:, :, 1) = [1 2 3; 4 5 6]; A(:, :, 2) = [1 0 0; 0 0 1]; What is the value of B{1} after executing each of the following statements?
    1. B = num2cell(A)
    2. B = num2cell(A, 1)
    3. B = num2cell(A, 2)
    4. B = num2cell(A, 3)
  5. Using 'mat2cell' for a matrix: What is the value of C{end} after executing the following statements? X = [1 2 3 4; 5 6 7 8; 9 10 11 12] C = mat2cell(X,[1 2],[1 3])
  6. Using 'struct2cell': Suppose that S is defined by the following statement: S = struct('name',{'Tim','Annie'},'age', {8,5}); What is the value of C after executing each of the following statements?
    1. C = struct2cell(S(1))
    2. C = struct2cell(S)
    3. C = struct2cell(S')
  7. Using 'deal': What is the value of C after executing the following statements? S = struct('name',{'Tim','Annie'},'age', {8,5}); [C{1:length(S)}] = deal(S.name);
  8. Using 'deal' for collecting info: What is the major purpose of the following statements? (Or what is contained in dirs?) dirInfo = dir(matlabroot); n = length(dirInfo); [fileAndDir{1:n}] = deal(dirInfo.name); dirs = fileAndDir([dirInfo.isdir])

MATLAB程式設計:入門篇