MATLAB ASCII double ASCII n char ASCII string
MATLAB 之 程應用:字元與字串 字串和ASCII double 指令: 檢視字串變數的儲存內容(即 ASCII 內碼) n char 指令: 將 ASCII 內碼轉回字串形式 範例: string 03. m n sentence = 'I''ve got a date!'; sentence. Ascii = double(sentence); %檢視 sentence 的 ASCII 碼 sentence 2 = char(sentence. Ascii) % 將 ASCII 碼轉回字串形式 sentence 2 = I've got a date! 6
MATLAB 之 程應用:字元與字串 ASCII: American Standard Code for Information Interchange http: //home. educities. edu. tw/wanker 742126/asm/ap 04. html 7
MATLAB 之 程應用:字元與字串的基本概念-eval n eval 指令特別適用於在 for - loop 內自動產生有規律的變數名稱 範例: string 08. m clear all for i = 3: 6 % 清除所有變數 eval(['x', int 2 str(i) , '= magic(' , int 2 str(i) , ') ; ']); end whos x* Name x 3 x 4 x 5 x 6 Size 3 x 3 4 x 4 5 x 5 6 x 6 Bytes Class 72 double array 128 double array 200 double array 288 double array Grand total is 86 elements using 688 bytes n x 3 , x 4 , x 5 , x 6 都是在 for - loop 中產生的變數,分別代表維度為 3× 3、4× 4、5× 5、6× 6 的魔方陣 12
MATLAB 之 程應用:字元與字串 字串的比較 strcmp 指令: 用於比較字串內容的異同 範例: string 16. m不相等回傳 0, 相等回傳 1 n str 1 = 'today'; str 2 = 'tomorrow'; str 3 = 'today'; out 1 = strcmp(str 1, str 2) out 2 = strcmp(str 1, str 3) out 1 = 0 out 2 = 1 % 比較兩字串 str 1 和 str 2 = 0 % 比較兩字串 str 1 和 str 3 = 1 20
MATLAB 之 程應用:字元與字串 字串的尋找及代換 n strrep 指令: 用於字串尋找及代換, 例如: new. String = strrep(string, pattern 2) n 範例: string 20. m string = '我最喜歡的運動是網球'; pattern = '網球'; pattern 2 = '足球'; new. String = strrep(string, pattern 2) new. String = 我最喜歡的運動是足球 24
MATLAB 之 程應用:字元與字串 字串的分解 n n strtok 指令: 根據一給定的分界字元(Delimiting Characters),將一字串 拆解成數個字串,預設分界字元為空白字元 strvcat 指令: 將拆解下來的字串內容(即儲存在字串變數 chopped 中) 加 到二維字元陣列 parsed 中 範例: string 21. m input_string = 'ee cs econ stat me'; remainder = input_string; parsed = ''; % 建立一空字元陣列 while (any(remainder)) [chopped, remainder] = strtok(remainder); parsed = strvcat(parsed, chopped); end parsed = ee cs econ stat me 25
MATLAB 之 程應用:字元與字串 Note n n xlate 在 control與 compiler toolbox 中 沒有此些 具箱便無法執行 29
MATLAB 之 程應用:字元與字串 寫一個 MATLAB 程式big 5 with. Back. Slash. m,列出所有包 含反斜線的大五碼中文字。共有幾個? function big 5 with. Back. Slash % 找出包含 "" 的大五碼中文字 range=hex 2 dec('a 440'): hex 2 dec('c 67 e'); offending. Char=''; k=0; for i=1: length(range) num. Rep=i-1+range(1); % fprintf('%d/%d ===> %sn', i, length(range), char(num. Rep)); str. Rep=dec 2 hex(num. Rep); if hex 2 dec(str. Rep(1: 2))==abs(offending. Char) | hex 2 dec(str. Rep(3: 4))==abs(offending. Char) k=k+1; %fprintf('"%s" = "%s" + "%s"n', char(num. Rep), char(hex 2 dec(str. Rep(1: 2))), char(hex 2 dec(str. Rep(3: 4)))); fprintf('%d. %sn', k, char(num. Rep)); end fprintf('大五碼常用字中,共有 %d 個字會包含「%s」的內碼!n', k, offending. Char); 32
MATLAB 之 程應用:字元與字串 請寫一段 MATLAB 程式 big 5 with. Vertical. Bar. m ,來印出 含有一直槓的所有中文字。 請問共有幾個這樣的中文字 ?只要在大五碼常用字的範圍內,找含有「7 C」這個 byte的中文字,就是答案 function big 5 with. Vertical. Bar. m % 找出包含 "|" 的大五碼中文字 range=hex 2 dec('a 440'): hex 2 dec('c 67 e'); offending. Char='|'; k=0; for i=1: length(range) num. Rep=i-1+range(1); % fprintf('%d/%d ===> %sn', i, length(range), char(num. Rep)); str. Rep=dec 2 hex(num. Rep); if hex 2 dec(str. Rep(1: 2))==abs(offending. Char) | hex 2 dec(str. Rep(3: 4))==abs(offending. Char) k=k+1; %fprintf('"%s" = "%s" + "%s"n', char(num. Rep), char(hex 2 dec(str. Rep(1: 2))), char(hex 2 dec(str. Rep(3: 4)))); fprintf('%d. %sn', k, char(num. Rep)); end fprintf('大五碼常用字中,共有 %d 個字會包含「%s」的內碼!n', k, offending. Char); 33
MATLAB 之 程應用:字元與字串 字串與數值的轉換 n num 2 str 指令: 將實數轉為字串 範例: string 27. m x = 0: 0. 1: 2*pi; y = sin(x); plot(x, y) str = ['leftarrow (', num 2 str(2. 5), ', ', num 2 str(sin(2. 5)), ')']; text(2. 5, sin(2. 5), str) 36
MATLAB 之 程應用:字元與字串 function out = capalize(in) %capalize: Capitalize each word of a given sentence % Usage: out = captalize(in) % Multiple spaces are reduced to a single space too. if nargin<1, selfdemo; return; end if isempty(in), out = ''; return; end i = 1; [token, rem] = strtok(in); while ~isempty(token) extracted{i} = token; i = i+1; [token, rem] = strtok(rem); end for i = 1: length(extracted), string = extracted{i}; new_string = [upper(string(1)), string(2: end)]; extracted{i} = new_string; end out = extracted{1}; for i = 2: length(extracted), out = [out, ' ', extracted{i}]; end % ====== Self demo function selfdemo input = 'this is A test for Me too'; output = feval(mfilename, input); fprintf('input = %sn', input); fprintf('output = %sn', output); 43
MATLAB 之 程應用:字元與字串 HW:補強 capalize. m n n n 當輸入是 ‘you are m. Y s. UNshine’時, 輸出字串是 ‘You Are My Sunshine’。 而非目前的‘You Are MY SUNshine’ 44
MATLAB 之 程應用:字元與字串 function token. List = split(str, delimiter) % Split a string based on a given delimiter % % token. List = split(str, delimiter) if nargin==0; selfdemo; return; end token. List = {}; remain = str; i = 1; while ~isempty(remain), [token, remain] = strtok(remain, delimiter); token. List{i} = token; i = i+1; end function selfdemo str='This-is-a-test'; token. List=feval(mfilename, str, '-'); str fprintf('After running "token. List=split(str, ''-'')": n'); token. List 46
MATLAB 之 程應用:字元與字串 countrows. m function [uniq_row, count] = countrows(A) % COUNTROWS Unique element counts for a given matrix. % [uniq_row, count] = countrow(A) % A: input matrix where each row is viewed as an "element" % uniq_row: sorted listing of unique row in A % count: counts for each returned unique row % If A is a cell array of strings, then the output uniq_row is also a % cell array of strings. if nargin == 0, selfdemo; return, end cellstr_input = 0; if iscellstr(A), A = char(A); cellstr_input = 1; end term = sortrows(A); tmp 1 = term; tmp 1(end+1, : ) = tmp 1(end, : ) + 1; tmp 2 = tmp 1(1: end-1, : ) - tmp 1(2: end, : ); index = find(sum(abs(tmp 2)') ~= 0); uniq_row = term(index, : ); count = diff([0, index])'; % Rearrange according to decending order of counts [count, index] = sort(count); count = flipud(count); index = flipud(index); uniq_row = uniq_row(index, : ); % Return cell array of string if given cell array of string if cellstr_input, uniq_row = cellstr(uniq_row); end % ======= Subfunction ===== function selfdemo A = ['一台'; '八爪'; '三光'; '人性'; '八爪'; '三光'; '八爪']; fprintf('Original string matrix: n'); disp(A); fprintf('After sorting and finding counts of unique rows: n'); [uniq_row, count] = countrows(A); for i = 1: length(count), fprintf('%s ==> %gn', uniq_row(i, : ), count(i)); end fprintf('Original cell array of strings: n'); A = {'abc'; 'acd'; 'a'; 'bc'; 'ad'; 'acd'}; disp(A); [uniq_row, count] = countrows(A); fprintf('After sorting and finding counts of unique rows: n'); for i = 1: length(count), fprintf('%s ==> %gn', uniq_row{i}, count(i)); end 48
MATLAB 之 程應用:字元與字串 str 2 ngram. m function ngram = str 2 ngram(str, n) % Extract n-gram from a given string or cell string % Usage: ngram = str 2 ngram(str, n) % str: a single string or a cell string if nargin<1; selfdemo; return; end if nargin<2; n=2; end if isstr(str) ngram=char(buffer 2(str, n, n-1)'); end if iscell(str) ngram=[]; for i=1: length(str) ngram=[ngram; str 2 ngram(str{i}, n)]; end % ====== Sub-function ====== function out = buffer 2(y, frame. Size, overlap) % BUFFER 2 Frame blocking, This is almost the same as % "buffer“ except that there is no leading zeros y = y(: ); step = frame. Size-overlap; frame. Count = floor((length(y)-overlap)/step); out = zeros(frame. Size, frame. Count); for i=1: frame. Count, start. Index = (i-1)*step+1; out(: , i) = y(start. Index: (start. Index+frame. Size-1)); end % ====== Self demo ===== function selfdemo str = {'網路上的芳鄰', '清華大學ABCD'}; for i=1: length(str) fprintf('str{%d}=%sn', i, str{i}); end trigram = feval(mfilename, str, 3); fprintf('Trigram of the above string is: n'); disp(trigram); 50
MATLAB 之 程應用:字元與字串 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). 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), tail_combinat]; out = [out; loop_out]; end 52
MATLAB 之 程應用:字元與字串 permute 2. m function out = permute 2(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: permute([2 3 4 5], 2) or permute('abcde‘, 3). 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 = permute(remainder, n-1); loop_out = [first*ones(size(remainder_permute, 1), remainder_permute]; out = [out; loop_out]; end 54
MATLAB 之 程應用:字元與字串 combine 與 permute 2 之比較 combine(‘abc’, 2) ab ac bc n permute 2('abc', 2) ab ac ba bc ca cb n n combine([1 2 3], 2) 12 13 23 32 permute 2([1 2 3], 2) 12 13 21 23 31 32 n 55
MATLAB 之 程應用:字元與字串 get. Ngram. Stat. m % 統計 n-gram (bigram, trigram 等)在一篇文章出現的次數 filename='news. txt'; string=fileread(filename); string=xlate(string); string=string(isbig 5(string)); % 將兩個 byte 合成一個單位 % 只保留中文常用字 for n=2: 5 fprintf('Collect %g-grams. . . n', n); ngram=str 2 ngram(string, n); fprintf('Sort %g %g-grams. . . n', size(ngram, 1), n); [uniq. Ngram, count] = countrows(ngram); end fprintf('Display %d distinct n-grams according to counts. . . n', size(uniq. Ngram, 1)); for i = 1: 10 fprintf('%d/%d: %s ===> %gn', i, size(uniq. Ngram, 1), uniq. Ngram(i, : ), count(i)); end 57
MATLAB 之 程應用:字元與字串 使用者自建的 fileread 函數 function contents = fileread(file. Name); %FILEREAD Read the contents of a file and put it into a cell string if nargin==0, selfdemo; return; end fid = fopen(file. Name); if fid<0, error('Cannot open file!'); end line. Num = 1; while 1 line = fgetl(fid); if ~isstr(line), break, end contents{line. Num} = xlate(line); line. Num = line. Num+1; end fclose(fid); if exist('contents')~=1 contents=[]; end % ====== self demo function selfdemo file. Name = [mfilename, '. m']; contents = feval(mfilename, file. Name); fprintf('The contents of "%s": n', mfilename); for i=1: length(contents), fprintf('%sn', contents{i}); end 58
MATLAB 之 程應用:字元與字串 使用者自建的 filewrite 函數 function filewrite(cell. Str, file. Name); %FILEWRITE Write a cell string to a file if nargin==0, selfdemo; return; end fid = fopen(file. Name, 'w'); if fid<0, error. Message=['Cannot open "', file. Name, '"!']; error(error. Message); end for i=1: length(cell. Str), fprintf(fid, '%srn', cell. Str{i}); end fclose(fid); % ====== self demo function selfdemo file. Name = [tempdir, 'test. txt']; fprintf('file. Name=%sn', file. Name); cell. Str={'This', 'a', 'test. '}; fprintf('cell. Str = '); disp(cell. Str); feval(mfilename, cell. Str, file. Name); fprintf('The contents of "cell. Str" have been written to "%s": n', file. Name); type(file. Name); 60
- Slides: 60