├── .editorconfig ├── .gitignore ├── README.md ├── Week2-Functions ├── 1-Recursive_Functions │ ├── digit_sum.m │ ├── fibor.m │ ├── ifact.m │ ├── igcd.m │ ├── palindrome.m │ ├── recursive_max.m │ ├── reversal.m │ ├── rfact.m │ ├── rgcd.m │ ├── sierpinski.m │ └── sierpinskiE.m ├── 2-Variable_Number_of_Arguments │ ├── distribute.m │ ├── find_first.m │ ├── name_value_pairs.m │ ├── print_all.m │ ├── print_num.m │ └── voters.m └── 3-Function_Handles_and_Nested_Functions │ ├── A.m │ ├── assignment_rule.m │ ├── first_nested_example.m │ ├── get_anon_handle.m │ ├── get_polynomial_handle.m │ ├── grader.m │ └── poly_fun.m ├── Week3-Potpourri ├── 4-Mixed_Mode_Arithmetic │ ├── CocaCola.png │ ├── binary_op_examples.mlx │ ├── edgy.m │ ├── matlab.png │ ├── mixit.m │ └── music.mat ├── 5-Linear_Equations │ ├── lin_reg.m │ └── voltage.m ├── 6-Live_Scripts │ ├── CO2 Mauna Loa.xlsx │ ├── CO2_Atmospheric_Concentrations.mlx │ ├── CRNSiteinMaunaLoa.jpg │ └── The_Old_Way.m └── 7-Error_Handling │ └── elem_prod.m ├── Week4-Write_Efficient_Code ├── 10-Vectorization │ ├── eigen_for.m │ ├── eigen_parfor.m │ ├── not_preallocatable_v1.m │ ├── not_preallocatable_v2.m │ ├── rooting_v4.m │ ├── rooting_v5.m │ ├── row2explicit.m │ ├── small2zero_v1.m │ ├── small2zero_v2.m │ └── stride_right.m ├── 8-Algorithmic_Complexity │ ├── binary_search.m │ ├── fast_flip.m │ ├── fibo.m │ ├── fibo_last.m │ ├── fibo_list.m │ ├── fibo_trace.m │ ├── fibocnt.m │ ├── graphConverter.m │ ├── matmul.m │ ├── max_clique_improved_1.m │ ├── max_clique_improved_2.m │ ├── max_clique_original.m │ ├── my_flip.m │ ├── my_search.m │ ├── reversal2.m │ ├── sn.mat │ ├── test_fast_flip.m │ └── test_matmul.m └── 9-Efficiency_in_Practice │ ├── follows.mat │ ├── max_same_follows_v1.m │ ├── max_same_follows_v2.m │ ├── rooting_v1.m │ ├── rooting_v2.m │ └── rooting_v3.m ├── Week5-Object_Oriented_Programming ├── 11-Introduction_to_OOP │ ├── BusinessContact.m │ ├── BusinessContact_v1.m │ ├── BusinessContact_v2.m │ ├── BusinessContact_v3.m │ ├── BusinessContact_v4.m │ ├── BusinessContact_v5.m │ ├── Contact.m │ ├── Contact_v1.m │ ├── Contact_v2.m │ ├── Contact_v3.m │ └── Contact_v4.m ├── 12-Handle_Classes │ ├── Akos.m │ ├── DList_v1.m │ ├── DList_v2.m │ ├── DList_v3.m │ ├── LetterNode.m │ ├── LetterNode_v1.m │ ├── LinkedNode_v1.m │ ├── LinkedNode_v2.m │ ├── LinkedNode_v3.m │ ├── Mike.m │ ├── TestClass_v1.m │ ├── TestClass_v2.m │ ├── TestClass_v3.m │ ├── Try_Akos.mlx │ ├── Try_Mike.mlx │ └── live_script_doubly_linked_list.mlx ├── 13-More_on_OOP │ ├── DList.m │ ├── DList_5.m │ ├── DList_6.m │ ├── DList_v3.m │ ├── DList_v4.m │ ├── DList_v5.m │ ├── DList_v6.m │ ├── LinkedNode.m │ ├── LinkedNode_v3.m │ ├── LinkedNode_v4.m │ ├── LinkedNode_v5.m │ ├── LinkedNode_v6.m │ ├── LinkedNode_v7.m │ ├── LinkedNode_v8.m │ ├── OrderedList._v1.m │ ├── OrderedList.m │ ├── OrderedList_v2.m │ ├── SortedNumber.m │ ├── SortedNumber_v1.m │ ├── SortedNumber_v2.m │ ├── SortedNumber_v3.m │ ├── SortedNumber_v4.m │ ├── SortedNumber_v5.m │ ├── SortedNumber_v6.m │ ├── live_script_5_3_1.mlx │ ├── live_script_5_3_2.mlx │ ├── live_script_5_3_3.mlx │ ├── live_script_5_3_4.mlx │ ├── live_script_5_3_5.mlx │ ├── live_script_5_3_6.mlx │ ├── live_script_5_3_7.mlx │ └── live_script_5_3_8.mlx ├── 14-Tying_it_All_Together │ ├── BusinessContact.m │ ├── BusinessContact_v5.m │ ├── BusinessContact_v6.m │ ├── BusinessContact_v7.m │ ├── BusinessContact_v8.m │ ├── Contact.m │ ├── Contact_v4.m │ ├── Contact_v5.m │ ├── Contact_v6.m │ ├── Contact_v7.m │ ├── Contact_v8.m │ ├── Contact_v9.m │ ├── DList.m │ ├── DList_6.m │ ├── LinkedNode.m │ ├── LinkedNode_v8.m │ ├── LinkedNode_v9.m │ ├── OrderedList.m │ ├── OrderedList_v2.m │ ├── SortedNumber.m │ ├── live_script_5_4_1.mlx │ ├── live_script_5_4_2.mlx │ ├── live_script_5_4_3.mlx │ ├── live_script_5_4_4.mlx │ ├── meld.m │ └── scratch.mlx └── 15-Final_Versions │ ├── BusinessContact.m │ ├── Contact.m │ ├── DList.m │ ├── LinkedNode.m │ ├── OrderedList.m │ └── SortedNumber.m ├── Week6-Applications ├── GlobalTemp.mat └── GlobalTemps.mlapp └── Week7-Final_Project ├── .gitattributes ├── CovidCountryData.m ├── CovidStateData.m ├── MyCovid19App.exe ├── MyCovid19App.mlapp ├── Statistics.m ├── covid_data.mat ├── icons ├── GitHub-icon.png ├── app-icon.png ├── clear-icon.png ├── default-setting-icon.png ├── email-icon.png └── paintbrush-icon.gif └── reference-solutions ├── 1.png ├── 10.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── 8.png └── 9.png /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs 2 | # editorconfig.org 3 | 4 | 5 | # Use top-most EditorConfig file 6 | root = true 7 | 8 | 9 | [*] 10 | # Unix-style newlines at the bottom of every file 11 | end_of_line = lf 12 | 13 | charset = utf-8 14 | 15 | # Remove any whitespace characters preceding newline characters 16 | trim_trailing_whitespace = true 17 | 18 | insert_final_newline = false 19 | 20 | # Tab indentation 21 | indent_style = space 22 | indent_size = 4 23 | 24 | # Give operators breathing room, but not brackets 25 | spaces_around_operators = true 26 | spaces_around_brackets = false 27 | 28 | 29 | [.js] 30 | quote_type = single 31 | curly_bracket_next_line = false 32 | 33 | 34 | [.html] 35 | quote_type = double 36 | 37 | 38 | [*.{diff, md}] 39 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.asv -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Coursera-Mastering-Programming-with-MATLAB 2 | ========================================== 3 | 4 | Instructor: [Mike Fitzpatrick](https://www.coursera.org/instructor/mikefitzpatrick) 5 | 6 | 7 | 8 | 12 | 13 |
9 | A collection of Course Files, Programming Assignments and Final Project for Mastering Programming with MATLAB. 10 | I took this online course on Coursera platform during August-September, 2021. (Certificate of Completion) 11 |
14 | 15 | > DISCLAIMER: Please do not simply copy the code without trying to solve the problems yourself in the first place. The author reserves all rights but does not be liable in any event (e.g., plagiarism) caused by the use of the program. Remember that one can only learn programming by doing it. Have fun coding!😃 16 | 17 | ### Overview 18 | This course is the second part of the specialization [MATLAB Programming for Engineers and Scientists Specialization](https://www.coursera.org/specializations/matlab-programming-engineers-scientists), offered by Vanderbilt University. 19 | It covers more advanced MATLAB programming concepts such as **recursion**, **vectorization**, **function handles**, **algorithm complexity and efficiency**, **error handling**, etc.. 20 | It also presents many powerful features of MATLAB including **object-oriented programming**, **graphical user interface design environment (App Designer)** and **Live Scripts**. 21 | 22 | --- 23 | 24 | ### Final Project 25 | 26 | In the final project of this course, I used object-oriented programming techniques and developed a MATLAB application with professional graphical user interfaces (GUIs) that visualize COVID-19 pandemic data from around the world. 27 | I did enjoy doing this project as it's a good chance to apply newly learned skills to a real world problem. :) 28 | The data obtained from the [Coronavirus Resource Center at the Johns Hopkins University](https://coronavirus.jhu.edu/map.html) is available from the `covid_data.mat` file. (currently including data up till January 30th, 2021) 29 | 30 |

31 | 32 |

33 | 34 | #### Features 35 | - Axes for displaying plots 36 | - A list box for countries 37 | - A list box for states/regions 38 | - A widget to select what data (Cases, Deaths or Both) to plot 39 | - A widget to display the option for cumulative or daily data 40 | - A widget to set the number of days for the moving average of the data (from 1 to 15) 41 | - A search box to find a specified country/state/region, so that the user does not have to scroll and find countries/states/regions manually in the list box 42 | - A button to clear the entered text and change to the default settings 43 | - A button to reset everything to the default settings 44 | - Date pickers to change the range of dates to plot 45 | - A toolbar for different functionalities 46 | 47 | #### How to run the program? 48 | - If you have MATLAB installed, open MATLAB and run the file `MyCovid19App.mlapp` 49 | - If you do not have MATLAB installed and use Windows on your machine, run the standalone executable `MyCovid19App.exe` 50 | 51 | --- 52 | 53 | ### Contact 54 | If you have any question or suggestion, feel free to contact me at huaming.huang.tw@gmail.com. Contributions are also welcomed. Please open a [pull-request](https://github.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/compare) or an [issue](https://github.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/issues/new) in this repository. 55 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/digit_sum.m: -------------------------------------------------------------------------------- 1 | function res = digit_sum(n) 2 | if n < 10 3 | res = n; 4 | else 5 | res = digit_sum(floor(n/10)) + rem(n,10); 6 | end 7 | end -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/fibor.m: -------------------------------------------------------------------------------- 1 | function f = fibor(n) 2 | if n == 1 3 | f = 1; 4 | elseif n == 2 5 | f = [ 1 1 ]; 6 | else 7 | f = fibor(n-1); % get the first n-1 elements 8 | f = [f f(end-1)+f(end)];% add together the last two elements 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/ifact.m: -------------------------------------------------------------------------------- 1 | function f = ifact(n) 2 | if ~isscalar(n) || n ~= fix(n) || n < 0 3 | error('non-negative integer scalar input expected'); 4 | end 5 | f = 1; 6 | for ii = 1:n 7 | f = f * ii; 8 | end 9 | end -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/igcd.m: -------------------------------------------------------------------------------- 1 | function d = igcd(x,y) 2 | 3 | if (~isscalar(x) || ~isscalar(y) || ... 4 | x ~= fix(x) || y ~= fix(y) || ... 5 | x < 0 || y < 0) 6 | error('x and y must be non-negative integers'); 7 | end 8 | 9 | a = max([x y]); 10 | b = min([x y]); 11 | while b ~= 0 12 | rem_count = rem_count + 1; 13 | r = rem(a,b); 14 | a = b; 15 | b = r; 16 | end 17 | d = a; 18 | end -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/palindrome.m: -------------------------------------------------------------------------------- 1 | function ok = palindrome(txt) 2 | if length(txt) <= 1 % if it is empty or has just one char, 3 | ok = true; % it is a palindrome. Base case. 4 | else % else 5 | ok = (txt(1) == txt(end) & ... % the first and last digits must be the same, and 6 | palindrome(txt(2:end-1))); % what's in the middle must be a palindrome too 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/recursive_max.m: -------------------------------------------------------------------------------- 1 | function mx = recursive_max(v) 2 | if length(v) == 1 3 | mx = v(1); 4 | else 5 | % Each recursion, v(2:end) becomes smaller by 1-element 6 | mx = bigger(v(1),recursive_max(v(2:end))); 7 | end 8 | end 9 | % Cannot use the max function. Use helper function to return the larger of 10 | % two element 11 | function c = bigger(a,b) 12 | c = a; 13 | if a < b 14 | c = b; 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/reversal.m: -------------------------------------------------------------------------------- 1 | function out = reversal(in) 2 | if length(in) <= 1 3 | out = in; 4 | else 5 | out = [ reversal(in(2:end)) in(1) ]; 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/rfact.m: -------------------------------------------------------------------------------- 1 | function f = rfact(n) 2 | if ~isscalar(n) || n ~= fix(n) || n < 0 3 | error('non-negative integer scalar input expected'); 4 | end 5 | if n == 0 6 | f = 1; 7 | else 8 | f = n * rfact(n-1); 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/rgcd.m: -------------------------------------------------------------------------------- 1 | function d = rgcd(x,y) % gcd = greatest common divisor 2 | if (~isscalar(x) || ~isscalar(y) || ... 3 | x ~= fix(x) || y ~= fix(y) || ... 4 | x < 0 || y < 0) 5 | error('x and y must be non-negative integers'); 6 | end 7 | 8 | a = max([x y]); 9 | b = min([x y]); 10 | 11 | if b == 0 12 | d = a; 13 | else 14 | d = rgcd(b,rem(a,b)); 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/sierpinski.m: -------------------------------------------------------------------------------- 1 | function sierpinski(depth) 2 | s = 1; c = [0;0]; % s = length of side, c = center 3 | clf; axis([c(1)-s/2,c(1)+s/2,c(2)-s/2,c(2)+s/2],'equal'); 4 | title(sprintf('Sierpinski Triangle with Depth = %d', depth)) 5 | hold on; 6 | sier(depth, s, c); 7 | hold off; 8 | end 9 | function sier(d, s, c) 10 | if d <= 0 % base case 11 | plot(c(1)-[s,0,-s,s]/2,c(2)-[s,-s,s,s]*sqrt(3)/4,'k'); 12 | else % recursive case 13 | s = s/2; % cuts size in half 14 | h = s*sqrt(3)/2; % height 15 | sier( d-1, s, c - [s;h]/2 ); % bottom left 16 | sier( d-1, s, c + [0;h]/2 ); % top middle 17 | sier( d-1, s, c + [s;-h]/2 ); % bottom right 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /Week2-Functions/1-Recursive_Functions/sierpinskiE.m: -------------------------------------------------------------------------------- 1 | function sierpinskiE(depth) 2 | s = 1; c = [0;0]; % s = length of side, c = center 3 | clf; axis([c(1)-s/2,c(1)+s/2,c(2)-s/2,c(2)+s/2],'equal'); 4 | title(sprintf('Sierpinski Triangle with Depth = %d', depth)) 5 | hold on; 6 | plot(1/2*[-1,0,1,-1],sqrt(3)/4*[-1,1,-1,-1],'r--'); 7 | pts = sier(depth, s, c, []); 8 | plot(pts(1,:),pts(2,:),'k-'); 9 | hold off; 10 | end 11 | function pts = sier(d, s, c, pts) 12 | if d <= 0 % base case 13 | pts = [pts, c + [[-s,0,s,-s,nan]/2;sqrt(3)*[-s,s,-s,-s,nan]/4]]; 14 | else % recursive case 15 | s = s/2; % cuts size in half 16 | h = s*sqrt(3)/2; % height 17 | pts = sier( d-1, s, c - [s;h]/2, pts ); % bottom left 18 | pts = sier( d-1, s, c + [0;h]/2, pts ); % top middle 19 | pts = sier( d-1, s, c + [s;-h]/2, pts ); % bottom right 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /Week2-Functions/2-Variable_Number_of_Arguments/distribute.m: -------------------------------------------------------------------------------- 1 | function varargout = distribute(v) 2 | for ii = 1:length(v) 3 | varargout{ii} = v(ii); 4 | end 5 | end -------------------------------------------------------------------------------- /Week2-Functions/2-Variable_Number_of_Arguments/find_first.m: -------------------------------------------------------------------------------- 1 | function index = find_first(v,e) 2 | if nargin == 0 3 | error('At least one argument is required') 4 | elseif nargin == 1 5 | e = 0; 6 | end 7 | index = 0; 8 | indices = find(v == e); 9 | if ~isempty(indices) 10 | index = indices(1); 11 | end 12 | end -------------------------------------------------------------------------------- /Week2-Functions/2-Variable_Number_of_Arguments/name_value_pairs.m: -------------------------------------------------------------------------------- 1 | function store = name_value_pairs(varargin) 2 | % Initialize an empty cell array 3 | store = {}; 4 | % If the input does not come in pairs (remainder = 1), return 5 | if rem(length(varargin),2) 6 | return; 7 | end 8 | 9 | for ii = 1:floor(length(varargin)/2) 10 | % The Name part must be a char. If not, return an empty cell 11 | if ~ischar(varargin{2*ii-1}) 12 | store = {}; 13 | return; 14 | end 15 | store{ii,1} = varargin{2*ii-1}; 16 | store{ii,2} = varargin{2*ii}; 17 | end 18 | end 19 | 20 | %{ 21 | function db = name_value_pairs(varargin) 22 | if (nargin == 0) | (rem(nargin,2) == 1) 23 | db = {} 24 | return 25 | end 26 | for i = 1 : 2 : nargin 27 | if ( ~isequal(class(varargin{i}),'char') ) 28 | db = {} 29 | return 30 | end 31 | end 32 | numRows = nargin/2 33 | numCols = 2 34 | db = cell(numRows, numCols) 35 | for i = 1 : numRows 36 | db{i,1} = varargin{(i - 1) * numCols + 1} 37 | db{i,2} = varargin{(i - 1) * numCols + 2} 38 | end 39 | end 40 | %} -------------------------------------------------------------------------------- /Week2-Functions/2-Variable_Number_of_Arguments/print_all.m: -------------------------------------------------------------------------------- 1 | function print_all(varargin) 2 | for ii = 1:nargin 3 | fprintf('Here is input argument number %d: %d\n', ii, varargin{ii}) 4 | end 5 | end -------------------------------------------------------------------------------- /Week2-Functions/2-Variable_Number_of_Arguments/print_num.m: -------------------------------------------------------------------------------- 1 | function out = print_num(format,varargin) 2 | out = ''; 3 | argindex = 1; 4 | skip = false; 5 | for ii = 1:length(format) 6 | if skip 7 | skip = false; 8 | else 9 | if format(ii) ~= '%' 10 | out(end+1) = format(ii); 11 | else 12 | if ii+1 > length(format) 13 | break; 14 | end 15 | if format(ii+1) == '%' 16 | out(end+1) = '%'; 17 | else 18 | if argindex >= nargin 19 | error('not enough input arguments'); 20 | end 21 | out = [out num2str(varargin{argindex},format(ii:ii+1))]; 22 | argindex = argindex + 1; 23 | end 24 | skip = true; 25 | end 26 | end 27 | end 28 | end -------------------------------------------------------------------------------- /Week2-Functions/2-Variable_Number_of_Arguments/voters.m: -------------------------------------------------------------------------------- 1 | function database = voters(database,varargin) 2 | % Get the length of the input database 3 | count = length(database); 4 | 5 | % Create a copy of the database. This will be the new database if input 6 | % is valid 7 | tmp = database; 8 | 9 | % Names and IDs come in pairs. Increment loop counter by 2 10 | for ii = 1:2:length(varargin) 11 | % Make sure the Name is a char or string 12 | if ischar(varargin{ii}) || isstring(varargin{ii}) 13 | count = count + 1; 14 | tmp(count).Name = string(varargin{ii}); 15 | % Make sure there is a valid ID 16 | if ii+1 <= length(varargin) && isnumeric(varargin{ii+1}) && round(varargin{ii+1}) == varargin{ii+1} 17 | tmp(count).ID = varargin{ii+1}; 18 | else 19 | % Not valid input. Return original database 20 | return; 21 | end 22 | else 23 | % Not valid input. Return orginal database 24 | return; 25 | end 26 | end 27 | % All inputs valid. Update database. 28 | database = tmp; 29 | end 30 | 31 | %{ 32 | function database = voters(db, varargin) 33 | database = db 34 | if (rem(length(varargin),2)) 35 | return 36 | end 37 | name = {} 38 | id = {} 39 | for i = 1 : (length(varargin)/2) 40 | if ( (ischar(varargin{i*2 - 1}) || isstring(varargin{i*2 - 1})) && isnumeric(varargin{i*2}) && (round(varargin{i*2}) == varargin{i*2}) ) 41 | name{end + 1} = string(varargin{i*2 - 1}) % append cell array 42 | id{end + 1} = varargin{i*2} 43 | else 44 | return 45 | end 46 | end 47 | temp_db = struct('Name', name, 'ID', id) 48 | if (isempty(db)) 49 | database = temp_db 50 | else 51 | database = [database, temp_db] % append structure array 52 | end 53 | end 54 | %} -------------------------------------------------------------------------------- /Week2-Functions/3-Function_Handles_and_Nested_Functions/A.m: -------------------------------------------------------------------------------- 1 | function A 2 | xA = 1; 3 | function B 4 | xB = 2; 5 | function C 6 | xC = 3; 7 | show('C','xA',xA) 8 | show('C','xB',xB) 9 | show('C','xC',xC) 10 | end % C 11 | show('B','xA',xA) 12 | show('B','xB',xB); 13 | C 14 | D 15 | end % B 16 | function D 17 | xD = 4; 18 | show('D','xA',xA); 19 | show('D','xD',xD); 20 | end % D 21 | show('A','xA',xA) 22 | B 23 | D 24 | end 25 | 26 | function show(funct,name,value) 27 | fprintf('in %s: %s = %d\n',funct,name,value); 28 | end -------------------------------------------------------------------------------- /Week2-Functions/3-Function_Handles_and_Nested_Functions/assignment_rule.m: -------------------------------------------------------------------------------- 1 | function circle_area = assignment_rule(r) 2 | calculate_area 3 | fprintf('Area of circle with radius %.1f = %.1f\n',r,circle_area) 4 | 5 | function calculate_area 6 | circle_area = pi*r^2; 7 | end 8 | end -------------------------------------------------------------------------------- /Week2-Functions/3-Function_Handles_and_Nested_Functions/first_nested_example.m: -------------------------------------------------------------------------------- 1 | function [y1, y2] = first_nested_example(x) 2 | c = 10; 3 | sub(c,x); 4 | y1 = inner(x); 5 | 6 | function out = inner(in) 7 | out = c*in; 8 | end 9 | 10 | c = 11; 11 | sub(c,x) 12 | y2 = inner(x); 13 | end 14 | 15 | function sub(in1,in2) 16 | fprintf('Multiplying %d times %d\n',in1,in2) 17 | end 18 | -------------------------------------------------------------------------------- /Week2-Functions/3-Function_Handles_and_Nested_Functions/get_anon_handle.m: -------------------------------------------------------------------------------- 1 | function fh = get_anon_handle(c) 2 | 3 | fh = @(x) c*x; 4 | 5 | end -------------------------------------------------------------------------------- /Week2-Functions/3-Function_Handles_and_Nested_Functions/get_polynomial_handle.m: -------------------------------------------------------------------------------- 1 | function fh = get_polynomial_handle(p) 2 | function polynomial = poly(x) 3 | polynomial = 0; 4 | for ii = 1:length(p) 5 | polynomial = polynomial + p(ii).*x.^(ii-1); 6 | end 7 | end 8 | 9 | fh = @poly; 10 | end 11 | 12 | %{ 13 | function fh = get_polynomial_handle(p) 14 | function polynomial = poly(x) 15 | % VECTORIZATION 16 | polynomial = sum( p(1:end).*x.^(0:length(p)-1) ) % p(1:end) = p 17 | end 18 | 19 | fh = @poly; 20 | end 21 | %} 22 | -------------------------------------------------------------------------------- /Week2-Functions/3-Function_Handles_and_Nested_Functions/grader.m: -------------------------------------------------------------------------------- 1 | function correct = grader(func1, func2, varargin) 2 | correct = true 3 | for i = 1 : length(varargin) 4 | in = varargin{i} 5 | if (~isequal(func1(in), func2(in))) 6 | correct = false 7 | return 8 | end 9 | end 10 | end -------------------------------------------------------------------------------- /Week2-Functions/3-Function_Handles_and_Nested_Functions/poly_fun.m: -------------------------------------------------------------------------------- 1 | function fh = poly_fun(p) 2 | function polynomial = poly(x) 3 | polynomial = sum(p .* x.^(0:length(p)-1)); 4 | end 5 | 6 | fh = @poly; 7 | end -------------------------------------------------------------------------------- /Week3-Potpourri/4-Mixed_Mode_Arithmetic/CocaCola.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week3-Potpourri/4-Mixed_Mode_Arithmetic/CocaCola.png -------------------------------------------------------------------------------- /Week3-Potpourri/4-Mixed_Mode_Arithmetic/binary_op_examples.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week3-Potpourri/4-Mixed_Mode_Arithmetic/binary_op_examples.mlx -------------------------------------------------------------------------------- /Week3-Potpourri/4-Mixed_Mode_Arithmetic/edgy.m: -------------------------------------------------------------------------------- 1 | % Edge Detection in Image Processing 2 | 3 | function out = edgy(in) 4 | % Get the size of the input image 5 | [r, c] = size(in); 6 | 7 | % Create an output array that is two rows and columns smaller 8 | out = zeros(r-2,c-2); 9 | 10 | % Use the size of the new array for looping 11 | [r, c] = size(out); 12 | 13 | % Convert to double for doing calculations 14 | in = double(in); 15 | 16 | % Create the horizontal and vertical edge detector filters (Sobel operator) 17 | ex = [-1 0 1; -2 0 2; -1 0 1]; 18 | ey = [1 2 1; 0 0 0; -1 -2 -1]; 19 | for ii = 1:r 20 | for jj = 1:c 21 | sx = in(ii:ii+2,jj:jj+2) .* ex; 22 | sy = in(ii:ii+2,jj:jj+2) .* ey; 23 | % Calculate the output pixel value 24 | out(ii,jj) = sqrt( sum(sum(sx(:)))^2 + sum(sum(sy(:)))^2 ); 25 | end 26 | end 27 | % Convert back to uint8 28 | out = uint8(out); 29 | end 30 | 31 | %{ 32 | function processed_image = edgy(original_image) 33 | sx = [-1 0 1; -2 0 2; -1 0 1]; 34 | d = conv2(original_image, sx, 'valid'); 35 | sy = [1 2 1; 0 0 0; -1 -2 -1]; 36 | e = conv2(original_image, sy, 'valid'); 37 | 38 | [rows columns] = size(e); 39 | 40 | for i = 1 : rows 41 | for j = 1 : columns 42 | processed_image(i,j) = uint8( sqrt( d(i,j)*d(i,j) + e(i,j)*e(i,j) ) ); 43 | end 44 | end 45 | end 46 | --- 47 | cc = imread('CocaCola.png'); 48 | figure 49 | imshow(cc); 50 | edg = edgy(cc); 51 | figure 52 | imshow(edg); 53 | %} -------------------------------------------------------------------------------- /Week3-Potpourri/4-Mixed_Mode_Arithmetic/matlab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week3-Potpourri/4-Mixed_Mode_Arithmetic/matlab.png -------------------------------------------------------------------------------- /Week3-Potpourri/4-Mixed_Mode_Arithmetic/mixit.m: -------------------------------------------------------------------------------- 1 | function s = mixit(S,w) 2 | if size(S,2) ~= length(w) 3 | s = []; 4 | else 5 | w = w(:); % make sure it is a column vector 6 | S = 2 * double(S) / (2^16 - 1) - 1; % scale and shift it to the interval [-1 1] 7 | s = S * w; % matrix multiplication does what we need 8 | if max(abs(s)) > 1 % make sure to stay within range 9 | s = s / max(abs(s)); 10 | end 11 | end 12 | end 13 | 14 | %{ 15 | function out = mixit(in1, in2) 16 | input_start = 0; 17 | input_end = 65535; 18 | output_start = -1; 19 | output_end = 1; 20 | 21 | in1_mapped = (double(in1) - input_start) .* ( (output_end - output_start) / (input_end - input_start) ) + output_start; 22 | out = in1_mapped * in2'; 23 | if (max(abs(out)) > 1) 24 | out = out / max(abs(out)); 25 | end 26 | end 27 | %} -------------------------------------------------------------------------------- /Week3-Potpourri/4-Mixed_Mode_Arithmetic/music.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week3-Potpourri/4-Mixed_Mode_Arithmetic/music.mat -------------------------------------------------------------------------------- /Week3-Potpourri/5-Linear_Equations/lin_reg.m: -------------------------------------------------------------------------------- 1 | function [a, b] = lin_reg(x,y) 2 | M = [x; ones(1,length(x))]'; 3 | sol = M \ y'; 4 | a = sol(1); 5 | b = sol(2); 6 | end -------------------------------------------------------------------------------- /Week3-Potpourri/5-Linear_Equations/voltage.m: -------------------------------------------------------------------------------- 1 | function sol = voltage(V,R) 2 | % Create the coeffecients matrix 3 | M = [ R(2)*R(7) + R(1)*R(2) + R(1)*R(7), -R(1)*R(2), 0; 4 | -R(3)*R(4)*R(8), R(4)*R(7)*R(8) + R(3)*R(4)*R(8) + R(3)*R(4)*R(7) + R(3)*R(7)*R(8), -R(3)*R(4)*R(7); 5 | 0, -R(5)*R(6), R(6)*R(8) + R(5)*R(6) + R(5)*R(8) ]; 6 | y = V * [R(2)*R(7); R(4)*R(7)*R(8); R(6)*R(8)]; 7 | 8 | % Use the backslash operator to solve the system of linear equations 9 | sol = M \ y; 10 | end -------------------------------------------------------------------------------- /Week3-Potpourri/6-Live_Scripts/CO2 Mauna Loa.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week3-Potpourri/6-Live_Scripts/CO2 Mauna Loa.xlsx -------------------------------------------------------------------------------- /Week3-Potpourri/6-Live_Scripts/CO2_Atmospheric_Concentrations.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week3-Potpourri/6-Live_Scripts/CO2_Atmospheric_Concentrations.mlx -------------------------------------------------------------------------------- /Week3-Potpourri/6-Live_Scripts/CRNSiteinMaunaLoa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week3-Potpourri/6-Live_Scripts/CRNSiteinMaunaLoa.jpg -------------------------------------------------------------------------------- /Week3-Potpourri/6-Live_Scripts/The_Old_Way.m: -------------------------------------------------------------------------------- 1 | %% Mauna Loa CarbonDioxide Concentrations 2 | % The Excel spreadsheet CO2 Mauna Loa.xlsx contains the CO2 data from 3 | % Hawaii provided by the Earth System Research Laboratory of the National 4 | % Oceanic and Atmospheric Administration (NOAA). 5 | 6 | %% Let's read the data in and plot it: 7 | co2 = xlsread("CO2 Mauna Loa.xlsx"); 8 | year = co2(:,3); 9 | CO2 = co2(:,4); 10 | plot(year,CO2,'g'); grid on; 11 | title("Monthly CO2 Concentrations"); 12 | xlabel("Year"); ylabel("CO2 (ppm)"); 13 | %% Smooth the data and plot it with the original data: 14 | window = 12; 15 | smoothed = smoothdata(CO2,'movmean',window); 16 | figure; plot(year,CO2,'g',year,smoothed,'k'); grid on; 17 | title(sprintf('Monthly CO2 Concentration and %d-month smoothing',window)); 18 | xlabel("Year"); ylabel("CO2 (ppm)"); 19 | legend('Monthly','Smoothed','location','best'); -------------------------------------------------------------------------------- /Week3-Potpourri/7-Error_Handling/elem_prod.m: -------------------------------------------------------------------------------- 1 | function out = element_prod(X,Y,r,n,c) 2 | out = X(r,n) * Y(n,x); 3 | end 4 | 5 | % Version 2 6 | %{ 7 | function out = element_prod(X,Y,r,n,c) 8 | try 9 | out = X(r,n) * Y(n,x); 10 | catch ME 11 | if isequal(ME.identifier,'MARLAB:badsubscript') 12 | [Xsize] = size(X); 13 | [Ysize] = size(Y); 14 | m1 = sprintf('Accessed X(%d,%d) and Y(%d,%d), but\n',r,n,n,c); 15 | m2 = sprintf(' sizes = X[%d,%d] and Y[%d,%d].',Xsize,Ysize); 16 | error([m1,m2]); 17 | end 18 | end 19 | end 20 | %} 21 | 22 | % Version 3 23 | %{ 24 | function out = element_prod(X,Y,r,n,c) 25 | try 26 | out = X(r,n) * Y(n,x); 27 | catch ME 28 | if isequal(ME.identifier,'MARLAB:badsubscript') 29 | [Xsize] = size(X); 30 | [Ysize] = size(Y); 31 | m1 = sprintf('Accessed X(%d,%d) and Y(%d,%d), but\n',r,n,n,c); 32 | m2 = sprintf(' sizes = X[%d,%d] and Y[%d,%d].',Xsize,Ysize); 33 | error('JMF:element_product:badsubscript',[m1,m2]); 34 | end 35 | end 36 | end 37 | %} 38 | 39 | % Version 4 40 | %{ 41 | function out = element_prod(X,Y,r,n,c) 42 | try 43 | out = X(r,n) * Y(n,x); 44 | catch ME 45 | if isequal(ME.identifier,'MARLAB:badsubscript') 46 | [Xsize] = size(X); 47 | [Ysize] = size(Y); 48 | m1 = sprintf('Accessed X(%d,%d) and Y(%d,%d), but\n',r,n,n,c); 49 | m2 = sprintf(' sizes = X[%d,%d] and Y[%d,%d].',Xsize,Ysize); 50 | MyE = MException('JMF:element_product:badsubscript',[m1,m2]); 51 | throw(MyE); 52 | end 53 | end 54 | end 55 | %} 56 | 57 | % Version 5 58 | %{ 59 | function out = element_prod(X,Y,r,n,c) 60 | try 61 | out = X(r,n) * Y(n,x); 62 | catch ME 63 | if isequal(ME.identifier,'MARLAB:badsubscript') 64 | [Xsize] = size(X); 65 | [Ysize] = size(Y); 66 | m1 = sprintf('Accessed X(%d,%d) and Y(%d,%d), but\n',r,n,n,c); 67 | m2 = sprintf(' sizes = X[%d,%d] and Y[%d,%d].',Xsize,Ysize); 68 | MyE = MException('JMF:element_product:badsubscript',[m1,m2]); 69 | throw(MyE); 70 | else 71 | rethrow(ME); % when error is not caught above 72 | end 73 | end 74 | end 75 | %} -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/eigen_for.m: -------------------------------------------------------------------------------- 1 | function a= eigen_for(A3D) 2 | a = zeros(1,size(A3D,1)); 3 | for ii = 1:length(a) 4 | a(ii) = max(abs(eig(squeeze(A3D(ii,:,:))))); 5 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/eigen_parfor.m: -------------------------------------------------------------------------------- 1 | function a= eigen_parfor(A3D) 2 | a = zeros(1,size(A3D,1)); 3 | parfor ii = 1:length(a) 4 | a(ii) = max(abs(eig(squeeze(A3D(ii,:,:))))); 5 | end 6 | 7 | -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/not_preallocatable_v1.m: -------------------------------------------------------------------------------- 1 | function A = not_preallocatable_v1(N) 2 | % from COMPUTER PROGRAMMING WITH MATLAB, 3rd Edition, 2015 3 | % by J. M. Fitzpatrick and A. Ledeczi 4 | % Chapter 2, Section 4.9 5 | ii = 0; 6 | while rand > 1/N 7 | ii = ii + 1; 8 | for jj = 1:N 9 | A(ii,jj) = ii + jj^2; 10 | end 11 | end 12 | 13 | 14 | -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/not_preallocatable_v2.m: -------------------------------------------------------------------------------- 1 | function A = not_preallocatable_v2(N) 2 | % from COMPUTER PROGRAMMING WITH MATLAB, 3rd Edition, 2015 3 | % by J. M. Fitzpatrick and A. Ledeczi 4 | % Chapter 2, Section 4.9 5 | ii = 0; 6 | while rand > 1/N 7 | ii = ii + 1; 8 | for jj = 1:N 9 | A(jj,ii) = ii + jj^2; 10 | end 11 | end 12 | A = A'; 13 | -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/rooting_v4.m: -------------------------------------------------------------------------------- 1 | function A = rooting_v4(v,w) 2 | A = zeros(length(v),length(w)); 3 | rv = nthroot(v,1:length(v)); 4 | rw = nthroot(w,1:length(v)); 5 | for ii = 1:length(v) 6 | for jj = 1:length(w) 7 | A(ii,jj) = rv(ii) * rw(jj); 8 | end 9 | end 10 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/rooting_v5.m: -------------------------------------------------------------------------------- 1 | function A = rooting_v5(v,w) 2 | rv = nthroot(v,1:length(v)); 3 | rw = nthroot(w,1:length(v)); 4 | A = rv' * rw; 5 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/row2explicit.m: -------------------------------------------------------------------------------- 1 | function A = row2explicit(A) 2 | for ii = 1:size(A,1) 3 | for jj = 1:size(A,2) 4 | if A(ii,jj) < A(ii,2) 5 | A(ii,jj) = 0; 6 | end 7 | end 8 | end 9 | end 10 | 11 | 12 | -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/small2zero_v1.m: -------------------------------------------------------------------------------- 1 | function A = small2zero_v1(A,limit) 2 | for ii = 1:size(A,1) 3 | for jj = 1:size(A,2) 4 | if A(ii,jj) < limit 5 | A(ii,jj) = 0; 6 | end 7 | end 8 | end 9 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/10-Vectorization/small2zero_v2.m: -------------------------------------------------------------------------------- 1 | function A = small2zero_v2(A,limit) 2 | A(A length(max_clq) 10 | max_clq = clq; 11 | end 12 | end 13 | else 14 | for ii = 1:length(graph{clique(1)}) % only consider nodes that the first node in the clique follows 15 | node = graph{clique(1)}(ii); 16 | if any(node == clique) % replace isempty(find(node == clique)) with ~any(node == clique) for better performance 17 | continue; 18 | end 19 | ok = true; % inlined check_clique: 20 | for jj = 1:length(clique) 21 | if ~any(clique(jj) == graph{node}) || ~any(node == graph{clique(jj)}) 22 | ok = false; 23 | break; 24 | end 25 | end 26 | if ok % check_clique(clq,node,graph) 27 | clq = max_clique_improved_1(graph,[clique node]); 28 | if length(clq) > length(max_clq) 29 | max_clq = clq; 30 | end 31 | end 32 | end 33 | end 34 | clique = max_clq; 35 | end 36 | 37 | %{ 38 | >>> load sn; 39 | >>> tic; max_clique_improved_1(sn); toc; 40 | Elapsed time is 5.185629 seconds. 41 | 42 | or 43 | 44 | >>> load sn; 45 | >>> new_sn = graphConverter(sn); 46 | >>> tic; max_clique_improved_1(new_sn); toc; 47 | Elapsed time is 1.393950 seconds. 48 | --- 49 | ans = 50 | 1769 1773 1774 1833 2222 51 | %} -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/max_clique_improved_2.m: -------------------------------------------------------------------------------- 1 | function clique = max_clique_improved_2(g,clique) 2 | if nargin < 2 3 | clique = []; 4 | end 5 | max_clq = clique; 6 | if isempty(clique) 7 | for ii = 1:length(g) 8 | clq = max_clique_improved_2(g,ii); 9 | if length(clq) > length(max_clq) 10 | max_clq = clq; 11 | end 12 | end 13 | else 14 | candidates = g{clique(1)}; % it is enough to check nodes that the first member of the clique follows 15 | candidates = candidates(g{clique(1)} > max(clique)); % since nodes are ordered, a potential new member must have a greater ID than current members 16 | for ii = 1:length(candidates) 17 | if check_clq(clique,candidates(ii),g) 18 | clq = max_clique_improved_2(g,[clique candidates(ii)]); 19 | if length(clq) > length(max_clq) 20 | max_clq = clq; 21 | end 22 | end 23 | end 24 | end 25 | clique = max_clq; 26 | end 27 | 28 | function ok = check_clq(clq,id,g) 29 | ok = false; 30 | if ~isempty(find(id == clq)) 31 | return; 32 | end 33 | for ii = 1:length(clq) 34 | if isempty(find(clq(ii) == g{id})) || isempty(find(id == g{clq(ii)})) 35 | return; 36 | end 37 | end 38 | ok = true; 39 | end 40 | 41 | %{ 42 | >>> load sn; 43 | >>> tic; max_clique_improved_2(sn); toc; 44 | Elapsed time is 1.296164 seconds. 45 | 46 | or 47 | 48 | >>> load sn; 49 | >>> new_sn = graphConverter(sn); 50 | >>> tic; max_clique_improved_2(new_sn); toc; 51 | Elapsed time is 0.561900 seconds. 52 | --- 53 | ans = 54 | 1769 1773 1774 1833 2222 55 | %} -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/max_clique_original.m: -------------------------------------------------------------------------------- 1 | function clique = max_clique_original(graph,clique) 2 | if nargin < 2 % originaly we call the function with just the graph 3 | clique = []; % hence, the clique is initialized to an empty vector 4 | end 5 | max_clq = clique; % max_clq will store the current largest clique 6 | if isempty(clique) % when we first call the function 7 | for ii = 1:length(graph) % we need to test potential cliques starting from every possible node 8 | clq = max_clique_original(graph,ii); 9 | if length(clq) > length(max_clq) % if the new one is larger than the current 10 | max_clq = clq; % we store the new one 11 | end 12 | end 13 | else 14 | for node = 1:length(graph) % we are in a recursive call now: we test every node as a new member 15 | if isempty(find(node == clique)) % unless it is already in the clique 16 | if check_clique(clique,node,graph) % if adding this node is still a clique 17 | clq = max_clique_original(graph,[clique node]); % we call ourself with the new expanded clique 18 | if length(clq) > length(max_clq) % if what we get is larger the curent max 19 | max_clq = clq; % we store the new one 20 | end 21 | end 22 | end 23 | end 24 | end 25 | clique = max_clq; % return the largest one 26 | end 27 | 28 | function ok = check_clique(clq,node,graph) % adding node to clq still a clique? 29 | ok = false; 30 | for ii = 1:length(clq) % for every current member 31 | if isempty(find(clq(ii) == graph{node})) || ... % the member must be on the follows list of the new guy 32 | isempty(find(node == graph{clq(ii)})) % the new guy must be on the follows list of the member 33 | return; 34 | end 35 | end 36 | ok = true; 37 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/my_flip.m: -------------------------------------------------------------------------------- 1 | function v = my_flip(v) 2 | for ii = 2:length(v) 3 | tmp = v(ii); 4 | for jj = ii:-1:2 5 | v(jj) = v(jj-1); 6 | end 7 | v(1) = tmp; 8 | end 9 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/my_search.m: -------------------------------------------------------------------------------- 1 | function index = linear_search(v,e) 2 | index = 0; 3 | for ii = 1:length(v) 4 | if v(ii) == e 5 | index = ii; 6 | return; 7 | end 8 | end 9 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/reversal2.m: -------------------------------------------------------------------------------- 1 | function v = reversal2(v) 2 | if length(v) > 1 3 | ii = round(length(v) / 2); 4 | v = [reversal2(v(ii+1:end)) reversal2(v(1:ii))]; 5 | end 6 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/sn.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week4-Write_Efficient_Code/8-Algorithmic_Complexity/sn.mat -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/test_fast_flip.m: -------------------------------------------------------------------------------- 1 | function test_fast_flip 2 | % Make a list of vector lengths: 3 | N = 1e6*(1:10); 4 | % Measure fast_flip time for a vector of each length: 5 | for ii = 1:length(N) 6 | t(ii) = timeit(@() fast_flip(1:N(ii))); 7 | fprintf('Time for %8d elements = %.4f\n',N(ii),t(ii)); 8 | end 9 | % Plot time versus input size with a line and asterisks: 10 | plot(N,t,N,t,'r*'); 11 | 12 | -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/8-Algorithmic_Complexity/test_matmul.m: -------------------------------------------------------------------------------- 1 | function t = test_matmul(M,matrix_class) 2 | %TEST_MATMUL matmul run time for MxM matrices 3 | % TEST_MATMUL(M) M is a vector of matrix 4 | % dimensions. Run times are returned and 5 | % are plotted versus M-cubed along with 6 | % a fit line of the form: a*M^3 + b. 7 | % 8 | % TEST_MATMUL(...,MATRIX_CLASS) MATRIX_CLASS 9 | % is a string giving the class of matrices 10 | % constructed as input to matmul. Default 11 | % is double. 12 | % 13 | 14 | if nargin < 1, M = 100*(1:10); end 15 | if nargin < 2, matrix_class = "double"; end 16 | 17 | max_val = 99; % <= 2 digits for inspecting small matrices 18 | t = zeros(length(M),1); 19 | for ii = 1:length(M) 20 | A = randi(max_val,M(ii),matrix_class); 21 | B = randi(max_val,M(ii),matrix_class); 22 | t(ii) = timeit(@() matmul(A,B)); 23 | fprintf('M = %d, t = %.4d\n',M(ii),t(ii)); 24 | end 25 | % Fit data to M^3 dependence 26 | p = polyfit(M.^3,t,1); % straight-line fit 27 | t_fit = polyval(p,M.^3); 28 | % Plot time points and straight-line fit 29 | plot(M.^3,t,'b*',M.^3,t_fit,'--'); 30 | grid on 31 | title_str = ... 32 | sprintf('MxM-matrix-multiplication run time vs M-cubed for %ss',matrix_class); 33 | title(title_str); 34 | xlabel('M^3'); 35 | ylabel('time (s)'); 36 | legend('data','fit','Location','SouthEast') 37 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/9-Efficiency_in_Practice/follows.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week4-Write_Efficient_Code/9-Efficiency_in_Practice/follows.mat -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/9-Efficiency_in_Practice/max_same_follows_v1.m: -------------------------------------------------------------------------------- 1 | function [people, follows] = max_same_follows_v1(following) 2 | people = []; 3 | num_follows = 0; 4 | for ii = 1:length(following)-1 5 | for jj = ii+1:length(following) 6 | tmp_follows = intersect(following{ii},following{jj}); 7 | n = length(tmp_follows); 8 | if n > num_follows 9 | num_follows = n; 10 | people = [ii jj]; 11 | follows = tmp_follows; 12 | end 13 | end 14 | end 15 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/9-Efficiency_in_Practice/max_same_follows_v2.m: -------------------------------------------------------------------------------- 1 | function [people, follows] = max_same_follows_v2(following) 2 | people = []; 3 | num_follows = 0; 4 | for ii = 1:length(following)-1 5 | if length(following{ii}) <= num_follows % skip if list 6 | continue; % is too short 7 | end 8 | for jj = ii+1:length(following) 9 | if length(following{jj}) <= num_follows % skip if list 10 | continue; % is too short 11 | end 12 | tmp_follows = intersect(following{ii},following{jj}); 13 | n = length(tmp_follows); 14 | if n > num_follows 15 | num_follows = n; 16 | people = [ii jj]; 17 | follows = tmp_follows; 18 | end 19 | end 20 | end 21 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/9-Efficiency_in_Practice/rooting_v1.m: -------------------------------------------------------------------------------- 1 | function A = rooting_v1(v,w) 2 | A = zeros(length(v),length(w)); 3 | for ii = 1:length(v) 4 | for jj = 1:length(w) 5 | A(ii,jj) = nthroot(v(ii),ii) * nthroot(w(jj),jj); 6 | end 7 | end 8 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/9-Efficiency_in_Practice/rooting_v2.m: -------------------------------------------------------------------------------- 1 | function A = rooting_v2(v,w) 2 | A = zeros(length(v),length(w)); 3 | for ii = 1:length(v) 4 | x = nthroot(v(ii),ii); 5 | for jj = 1:length(w) 6 | A(ii,jj) = x * nthroot(w(jj),jj); 7 | end 8 | end 9 | end -------------------------------------------------------------------------------- /Week4-Write_Efficient_Code/9-Efficiency_in_Practice/rooting_v3.m: -------------------------------------------------------------------------------- 1 | function A = rooting_v3(v,w) 2 | A = zeros(length(v),length(w)); 3 | rw = zeros(1,length(w)); 4 | for jj = 1:length(w) 5 | rw(jj) = nthroot(w(jj),jj); 6 | end 7 | for ii = 1:length(v) 8 | x = nthroot(v(ii),ii); 9 | for jj = 1:length(w) 10 | A(ii,jj) = x * rw(jj); 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/BusinessContact.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact 2 | properties 3 | Company 4 | Fax 5 | end 6 | methods 7 | function obj = BusinessContact(cname,lname,fname,phone,f) 8 | if nargin < 5, f = ""; end 9 | if nargin < 4, phone = ""; end 10 | if nargin < 3, fname = ""; end 11 | if nargin < 2, lname = ""; end 12 | if nargin < 1, cname = ""; end 13 | obj@Contact(lname,fname,phone); 14 | obj.Company = string(cname); 15 | obj.Fax = string(f); 16 | end 17 | function obj = set.Company(obj,cname) 18 | obj.Company = string(cname); 19 | end 20 | function obj = set.Fax(obj,f) 21 | obj.Fax = string(f); 22 | end 23 | end 24 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/BusinessContact_v1.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact 2 | properties 3 | Company 4 | Fax 5 | end 6 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/BusinessContact_v2.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact 2 | properties 3 | Company 4 | Fax 5 | end 6 | methods 7 | function obj = BusinessContact(cname,lname,fname,phone,f) 8 | obj.LastName = string(lname); 9 | obj.FirstName = string(fname); 10 | obj.PhoneNumber = string(phone); 11 | obj.Company = string(cname); 12 | obj.Fax = string(f); 13 | end 14 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/BusinessContact_v3.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact 2 | properties 3 | Company 4 | Fax 5 | end 6 | methods 7 | function obj = BusinessContact(cname,lname,fname,phone,f) 8 | obj@Contact(lname,fname,phone); 9 | obj.Company = string(cname); 10 | obj.Fax = string(f); 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/BusinessContact_v4.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact 2 | properties 3 | Company 4 | Fax 5 | end 6 | methods 7 | function obj = BusinessContact(cname,lname,fname,phone,f) 8 | if nargin < 5 f = ""; end 9 | if nargin < 4 phone = ""; end 10 | if nargin < 3 fname = ""; end 11 | if nargin < 2 lname = ""; end 12 | if nargin < 1 cname = ""; end 13 | obj@Contact(lname,fname,phone); 14 | obj.Company = string(cname); 15 | obj.Fax = string(f); 16 | end 17 | end 18 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/BusinessContact_v5.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact 2 | properties 3 | Company 4 | Fax 5 | end 6 | methods 7 | function obj = BusinessContact(cname,lname,fname,phone,f) 8 | if nargin < 5 f = ""; end 9 | if nargin < 4 phone = ""; end 10 | if nargin < 3 fname = ""; end 11 | if nargin < 2 lname = ""; end 12 | if nargin < 1 cname = ""; end 13 | obj@Contact(lname,fname,phone); 14 | obj.Company = string(cname); 15 | obj.Fax = string(f); 16 | end 17 | function obj = set.Company(obj,cname) 18 | obj.Company = string(cname); 19 | end 20 | function obj = set.Fax(obj,f) 21 | obj.Fax = string(f); 22 | end 23 | end 24 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/Contact.m: -------------------------------------------------------------------------------- 1 | classdef Contact 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | if nargin < 3, phone = ""; end 10 | if nargin < 2, fname = ""; end 11 | if nargin < 1, lname = ""; end 12 | obj.LastName = string(lname); 13 | obj.FirstName = string(fname); 14 | obj.PhoneNumber = string(phone); 15 | end 16 | function obj = set.LastName(obj,lname) 17 | obj.LastName = string(lname); 18 | end 19 | function obj = set.FirstName(obj,fname) 20 | obj.FirstName = string(fname); 21 | end 22 | function obj = set.PhoneNumber(obj,phone) 23 | obj.PhoneNumber = string(phone); 24 | end 25 | function printName(obj) 26 | fprintf('%s %s\n',obj.FirstName,obj.LastName) 27 | end 28 | end 29 | end 30 | 31 | 32 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/Contact_v1.m: -------------------------------------------------------------------------------- 1 | classdef Contact 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | end 8 | 9 | 10 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/Contact_v2.m: -------------------------------------------------------------------------------- 1 | classdef Contact 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | obj.LastName = string(lname); 10 | obj.FirstName = string(fname); 11 | obj.PhoneNumber = string(phone); 12 | end 13 | end 14 | end 15 | 16 | 17 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/Contact_v3.m: -------------------------------------------------------------------------------- 1 | classdef Contact 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | obj.LastName = string(lname); 10 | obj.FirstName = string(fname); 11 | obj.PhoneNumber = string(phone); 12 | end 13 | function obj = set.LastName(obj,lname) 14 | obj.LastName = string(lname); 15 | end 16 | function obj = set.FirstName(obj,fname) 17 | obj.FirstName = string(fname); 18 | end 19 | function obj = set.PhoneNumber(obj,phone) 20 | obj.PhoneNumber = string(phone); 21 | end 22 | end 23 | end 24 | 25 | 26 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/11-Introduction_to_OOP/Contact_v4.m: -------------------------------------------------------------------------------- 1 | classdef Contact 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | if nargin < 3, phone = ""; end 10 | if nargin < 2, fname = ""; end 11 | if nargin < 1, lname = ""; end 12 | obj.LastName = string(lname); 13 | obj.FirstName = string(fname); 14 | obj.PhoneNumber = string(phone); 15 | end 16 | function obj = set.LastName(obj,lname) 17 | obj.LastName = string(lname); 18 | end 19 | function obj = set.FirstName(obj,fname) 20 | obj.FirstName = string(fname); 21 | end 22 | function obj = set.PhoneNumber(obj,phone) 23 | obj.PhoneNumber = string(phone); 24 | end 25 | end 26 | end 27 | 28 | 29 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/Akos.m: -------------------------------------------------------------------------------- 1 | classdef Akos < handle 2 | properties 3 | card = 'Jack of Spades' 4 | end 5 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/DList_v1.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle 2 | properties 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | 9 | function list = DList() 10 | list.Head = []; 11 | list.Tail = []; 12 | list.Length = 0; 13 | end 14 | 15 | function insert(list,node) 16 | if ~isempty(node.Owner) 17 | if node.Owner ~= list % New node is in another list, 18 | node.Owner.remove(node); % so we need to remove it. 19 | else 20 | return; % New node is already in this list, 21 | end % so do nothing. 22 | end 23 | if list.Length == 0 % If the list is empty, 24 | list.Head = node; % put new node at the head, 25 | else 26 | list.Tail.Next = node; % else, point tail node at it. 27 | end 28 | node.Next = []; % New node is at the end. 29 | node.Prev = list.Tail; % Previous node is old tail node. 30 | list.Tail = node; % Make Tail node point at new node. 31 | list.Length = list.Length + 1; 32 | node.Owner = list; 33 | end % insert 34 | 35 | function remove(list,node) 36 | if isempty(node) || node.Owner ~= list 37 | error('node is not in the list'); 38 | end 39 | if ~isempty(node.Prev) % If a node precedes the current node, 40 | node.Prev.Next = node.Next; % make preceding node point to the node 41 | else % that follows the current node, 42 | list.Head = node.Next; % else make Head point to it. 43 | end 44 | if ~isempty(node.Next) % If a node follows the current node, 45 | node.Next.Prev = node.Prev; % make its prev point to the node that 46 | else % that precedes the current node 47 | list.Tail = node.Prev; % else make previous node be the tail. 48 | end 49 | list.Length = list.Length - 1; 50 | node.Next = []; 51 | node.Prev = []; 52 | node.Owner = []; 53 | end % remove 54 | 55 | function displayList(list) 56 | item = list.Head; 57 | while ~isempty(item) 58 | item.disp 59 | item = item.Next; 60 | end 61 | end 62 | end 63 | end 64 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/DList_v2.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle 2 | properties (Access = private) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | 9 | function list = DList() 10 | list.Head = []; 11 | list.Tail = []; 12 | list.Length = 0; 13 | end 14 | 15 | function insert(list,node) 16 | if ~isempty(node.Owner) 17 | if node.Owner ~= list % New node is in another list, 18 | node.Owner.remove(node); % so we need to remove it. 19 | else 20 | return; % New node is already in this list, 21 | end % so do nothing. 22 | end 23 | if list.Length == 0 % If the list is empty, 24 | list.Head = node; % put new node at the head, 25 | else 26 | list.Tail.Next = node; % else, point tail node at it. 27 | end 28 | node.Next = []; % New node is at the end. 29 | node.Prev = list.Tail; % Previous node is old tail node. 30 | list.Tail = node; % Make Tail node point at new node. 31 | list.Length = list.Length + 1; 32 | node.Owner = list; 33 | end % insert 34 | 35 | function remove(list,node) 36 | if isempty(node) || node.Owner ~= list 37 | error('node is not in the list'); 38 | end 39 | if ~isempty(node.Prev) % If a node precedes the current node, 40 | node.Prev.Next = node.Next; % make preceding node point to the node 41 | else % that follows the current node, 42 | list.Head = node.Next; % else make Head point to it. 43 | end 44 | if ~isempty(node.Next) % If a node follows the current node, 45 | node.Next.Prev = node.Prev; % make its prev point to the node that 46 | else % that precedes the current node 47 | list.Tail = node.Prev; % else make previous node be the tail. 48 | end 49 | list.Length = list.Length - 1; 50 | node.Next = []; 51 | node.Prev = []; 52 | node.Owner = []; 53 | end % remove 54 | 55 | function displayList(list) 56 | item = list.Head; 57 | while ~isempty(item) 58 | item.disp 59 | item = item.Next; 60 | end 61 | end 62 | end 63 | end 64 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/DList_v3.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle 2 | properties (Access = private) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | 9 | function lng = length(list) 10 | lng = list.Length; 11 | end 12 | 13 | function list = DList() 14 | list.Head = []; 15 | list.Tail = []; 16 | list.Length = 0; 17 | end 18 | 19 | function insert(list,node) 20 | if ~isempty(node.Owner) 21 | if node.Owner ~= list % New node is in another list, 22 | node.Owner.remove(node); % so we need to remove it. 23 | else 24 | return; % New node is already in this list, 25 | end % so do nothing. 26 | end 27 | if list.Length == 0 % If the list is empty, 28 | list.Head = node; % put new node at the head, 29 | else 30 | list.Tail.Next = node; % else, point tail node at it. 31 | end 32 | node.Next = []; % New node is at the end. 33 | node.Prev = list.Tail; % Previous node is old tail node. 34 | list.Tail = node; % Make Tail node point at new node. 35 | list.Length = list.Length + 1; 36 | node.Owner = list; 37 | end % insert 38 | 39 | function remove(list,node) 40 | if isempty(node) || node.Owner ~= list 41 | error('node is not in the list'); 42 | end 43 | if ~isempty(node.Prev) % If a node precedes the current node, 44 | node.Prev.Next = node.Next; % make preceding node point to the node 45 | else % that follows the current node, 46 | list.Head = node.Next; % else make Head point to it. 47 | end 48 | if ~isempty(node.Next) % If a node follows the current node, 49 | node.Next.Prev = node.Prev; % make its prev point to the node that 50 | else % that precedes the current node 51 | list.Tail = node.Prev; % else make previous node be the tail. 52 | end 53 | list.Length = list.Length - 1; 54 | node.Next = []; 55 | node.Prev = []; 56 | node.Owner = []; 57 | end % remove 58 | 59 | function displayList(list) 60 | item = list.Head; 61 | while ~isempty(item) 62 | item.disp 63 | item = item.Next; 64 | end 65 | end 66 | end 67 | end 68 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/LetterNode.m: -------------------------------------------------------------------------------- 1 | classdef LetterNode < LinkedNode 2 | properties 3 | Letter 4 | end 5 | methods 6 | function obj = LetterNode(l) 7 | if nargin < 1 8 | obj.Letter = ' '; 9 | else 10 | obj.Letter = l; 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/LetterNode_v1.m: -------------------------------------------------------------------------------- 1 | classdef LetterNode < LinkedNode 2 | properties 3 | Letter 4 | end 5 | methods 6 | function obj = LetterNode(l) 7 | if nargin < 1 8 | obj.Letter = ' '; 9 | else 10 | obj.Letter = l; 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/LinkedNode_v1.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle 2 | properties 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods 8 | function node = LinkedNode() 9 | node.Prev = []; 10 | node.Next = []; 11 | node.Owner = []; 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/LinkedNode_v2.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle 2 | properties (Access = private) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods 8 | function node = LinkedNode() 9 | node.Prev = []; 10 | node.Next = []; 11 | node.Owner = []; 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/LinkedNode_v3.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle 2 | properties (Access = ?DList) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods 8 | function node = LinkedNode() 9 | node.Prev = []; 10 | node.Next = []; 11 | node.Owner = []; 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/Mike.m: -------------------------------------------------------------------------------- 1 | classdef Mike 2 | properties 3 | card = 'Jack of Spades' 4 | end 5 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/TestClass_v1.m: -------------------------------------------------------------------------------- 1 | classdef TestClass 2 | properties 3 | Value 4 | end 5 | methods 6 | function obj = TestClass(val) 7 | if nargin < 1 8 | obj.Value = 0; 9 | else 10 | obj.Value = val; 11 | end 12 | end 13 | function set_value(obj,val) 14 | obj.Value = val; 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/TestClass_v2.m: -------------------------------------------------------------------------------- 1 | classdef TestClass 2 | properties 3 | Value 4 | end 5 | methods 6 | function obj = TestClass(val) 7 | if nargin < 1 8 | obj.Value = 0; 9 | else 10 | obj.Value = val; 11 | end 12 | end 13 | function obj = set_value(obj,val) 14 | obj.Value = val; 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/TestClass_v3.m: -------------------------------------------------------------------------------- 1 | classdef TestClass < handle 2 | properties 3 | Value 4 | end 5 | methods 6 | function obj = TestClass(val) 7 | if nargin < 1 8 | obj.Value = 0; 9 | else 10 | obj.Value = val; 11 | end 12 | end 13 | function set_value(obj,val) 14 | obj.Value = val; 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/Try_Akos.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/12-Handle_Classes/Try_Akos.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/Try_Mike.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/12-Handle_Classes/Try_Mike.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/12-Handle_Classes/live_script_doubly_linked_list.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/12-Handle_Classes/live_script_doubly_linked_list.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/DList.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v6 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function disp(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp(); 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/DList_5.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v5 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function displayList(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/DList_6.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v6 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function disp(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/DList_v3.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v3 2 | properties (Access = private) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function insert(list,node) 19 | if ~isempty(node.Owner) 20 | if node.Owner ~= list % New node is in another list, 21 | node.Owner.remove(node); % so we need to remove it. 22 | else 23 | return; % New node is already in this list, 24 | end % so do nothing. 25 | end 26 | if list.Length == 0 % If the list is empty, 27 | list.Head = node; % put new node at the head, 28 | else 29 | list.Tail.Next = node; % else, point tail node at it. 30 | end 31 | node.Next = []; % New node is at the end. 32 | node.Prev = list.Tail; % Previous node is old tail node. 33 | list.Tail = node; % Make Tail node point at new node. 34 | list.Length = list.Length + 1; 35 | node.Owner = list; 36 | end % insert 37 | 38 | function remove(list,node) 39 | if isempty(node) || node.Owner ~= list 40 | error('node is not in the list'); 41 | end 42 | if ~isempty(node.Prev) % If a node precedes the current node, 43 | node.Prev.Next = node.Next; % make preceding node point to the node 44 | else % that follows the current node, 45 | list.Head = node.Next; % else make Head point to it. 46 | end 47 | if ~isempty(node.Next) % If a node follows the current node, 48 | node.Next.Prev = node.Prev; % make its prev point to the node that 49 | else % that precedes the current node 50 | list.Tail = node.Prev; % else make previous node be the tail. 51 | end 52 | list.Length = list.Length - 1; 53 | node.Next = []; 54 | node.Prev = []; 55 | node.Owner = []; 56 | end % remove 57 | 58 | function displayList(list) 59 | item = list.Head; 60 | while ~isempty(item) 61 | item.disp 62 | item = item.Next; 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/DList_v4.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v4 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function insert(list,node) 19 | if ~isempty(node.Owner) 20 | if node.Owner ~= list % New node is in another list, 21 | node.Owner.remove(node); % so we need to remove it. 22 | else 23 | return; % New node is already in this list, 24 | end % so do nothing. 25 | end 26 | if list.Length == 0 % If the list is empty, 27 | list.Head = node; % put new node at the head, 28 | else 29 | list.Tail.Next = node; % else, point tail node at it. 30 | end 31 | node.Next = []; % New node is at the end. 32 | node.Prev = list.Tail; % Previous node is old tail node. 33 | list.Tail = node; % Make Tail node point at new node. 34 | list.Length = list.Length + 1; 35 | node.Owner = list; 36 | end % insert 37 | 38 | function remove(list,node) 39 | if isempty(node) || node.Owner ~= list 40 | error('node is not in the list'); 41 | end 42 | if ~isempty(node.Prev) % If a node precedes the current node, 43 | node.Prev.Next = node.Next; % make preceding node point to the node 44 | else % that follows the current node, 45 | list.Head = node.Next; % else make Head point to it. 46 | end 47 | if ~isempty(node.Next) % If a node follows the current node, 48 | node.Next.Prev = node.Prev; % make its prev point to the node that 49 | else % that precedes the current node 50 | list.Tail = node.Prev; % else make previous node be the tail. 51 | end 52 | list.Length = list.Length - 1; 53 | node.Next = []; 54 | node.Prev = []; 55 | node.Owner = []; 56 | end % remove 57 | 58 | function displayList(list) 59 | item = list.Head; 60 | while ~isempty(item) 61 | item.disp 62 | item = item.Next; 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/DList_v5.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v5 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function displayList(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/DList_v6.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v6 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function disp(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/LinkedNode.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v8 2 | properties (Access = {?DList ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | disp(a) 15 | end 16 | methods 17 | function node = LinkedNode() 18 | node.Prev = []; 19 | node.Next = []; 20 | node.Owner = []; 21 | end 22 | function delete(node) 23 | if ~isempty(node.Owner) 24 | node.Owner.remove(node); 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/LinkedNode_v3.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v3 2 | properties (Access = ?DList) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods 8 | function node = LinkedNode() 9 | node.Prev = []; 10 | node.Next = []; 11 | node.Owner = []; 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/LinkedNode_v4.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v4 2 | properties (Access = ?DList) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | end 10 | methods 11 | function node = LinkedNode() 12 | node.Prev = []; 13 | node.Next = []; 14 | node.Owner = []; 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/LinkedNode_v5.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v5 2 | properties (Access = ?DList) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | end 15 | methods 16 | function node = LinkedNode() 17 | node.Prev = []; 18 | node.Next = []; 19 | node.Owner = []; 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/LinkedNode_v6.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v6 2 | properties (Access = {?DList, ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | end 15 | methods 16 | function node = LinkedNode() 17 | node.Prev = []; 18 | node.Next = []; 19 | node.Owner = []; 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/LinkedNode_v7.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v7 2 | properties (Access = {?DList ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | end 15 | methods 16 | function node = LinkedNode() 17 | node.Prev = []; 18 | node.Next = []; 19 | node.Owner = []; 20 | end 21 | function delete(node) 22 | if ~isempty(node.Owner) 23 | node.Owner.remove(node); 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/LinkedNode_v8.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v8 2 | properties (Access = {?DList ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | disp(a) 15 | end 16 | methods 17 | function node = LinkedNode() 18 | node.Prev = []; 19 | node.Next = []; 20 | node.Owner = []; 21 | end 22 | function delete(node) 23 | if ~isempty(node.Owner) 24 | node.Owner.remove(node); 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/OrderedList._v1.m: -------------------------------------------------------------------------------- 1 | classdef OrderedList < DList % OrderedList_v1 2 | methods 3 | function insert(list,node) 4 | if ~isempty(node.Owner) 5 | if node.Owner ~= list 6 | node.Owner.remove(node); 7 | else 8 | return; 9 | end 10 | end 11 | node.Owner = list; 12 | list.Length = list.Length + 1; 13 | if isempty(list.Head) 14 | list.Head = node; 15 | list.Tail = node; 16 | node.Prev = []; 17 | node.Next = []; 18 | else 19 | cur = list.Head; 20 | prev = []; 21 | while ~isempty(cur) && node > cur 22 | prev = cur; 23 | cur = cur.Next; 24 | end 25 | if isempty(prev) 26 | node.Next = list.Head; 27 | node.Prev = []; 28 | list.Head.Prev = node; 29 | list.Head = node; 30 | else 31 | prev.Next = node; 32 | node.Prev = prev; 33 | node.Next = cur; 34 | if isempty(cur) 35 | list.Tail = node; 36 | else 37 | cur.Prev = node; 38 | end 39 | end 40 | end 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/OrderedList.m: -------------------------------------------------------------------------------- 1 | classdef OrderedList < DList % OrderedList_v2 2 | methods 3 | function insert(list,node) 4 | if ~isempty(node.Owner) 5 | if node.Owner ~= list 6 | node.Owner.remove(node); 7 | else 8 | return; 9 | end 10 | end 11 | node.Owner = list; 12 | list.Length = list.Length + 1; 13 | if isempty(list.Head) 14 | list.Head = node; 15 | list.Tail = node; 16 | node.Prev = []; 17 | node.Next = []; 18 | else 19 | cur = list.Head; 20 | prev = []; 21 | while ~isempty(cur) && node > cur 22 | prev = cur; 23 | cur = cur.Next; 24 | end 25 | if isempty(prev) 26 | node.Next = list.Head; 27 | node.Prev = []; 28 | list.Head.Prev = node; 29 | list.Head = node; 30 | else 31 | prev.Next = node; 32 | node.Prev = prev; 33 | node.Next = cur; 34 | if isempty(cur) 35 | list.Tail = node; 36 | else 37 | cur.Prev = node; 38 | end 39 | end 40 | end 41 | end 42 | function disp(list) 43 | disp('OrderedList containing:'); 44 | item = list.Head; 45 | while ~isempty(item) 46 | item.disp(); 47 | item = item.Next; 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/OrderedList_v2.m: -------------------------------------------------------------------------------- 1 | classdef OrderedList < DList % OrderedList_v2 2 | methods 3 | function insert(list,node) 4 | if ~isempty(node.Owner) 5 | if node.Owner ~= list 6 | node.Owner.remove(node); 7 | else 8 | return; 9 | end 10 | end 11 | node.Owner = list; 12 | list.Length = list.Length + 1; 13 | if isempty(list.Head) 14 | list.Head = node; 15 | list.Tail = node; 16 | node.Prev = []; 17 | node.Next = []; 18 | else 19 | cur = list.Head; 20 | prev = []; 21 | while ~isempty(cur) && node > cur 22 | prev = cur; 23 | cur = cur.Next; 24 | end 25 | if isempty(prev) 26 | node.Next = list.Head; 27 | node.Prev = []; 28 | list.Head.Prev = node; 29 | list.Head = node; 30 | else 31 | prev.Next = node; 32 | node.Prev = prev; 33 | node.Next = cur; 34 | if isempty(cur) 35 | list.Tail = node; 36 | else 37 | cur.Prev = node; 38 | end 39 | end 40 | end 41 | end 42 | function disp(list) 43 | disp('OrderedList containing:'); 44 | item = list.Head; 45 | while ~isempty(item) 46 | item.disp(); 47 | item = item.Next; 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/SortedNumber.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v6 2 | properties 3 | Value (1,1) {mustBeNumeric} = 0 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n = 0 9 | end 10 | node.Value = n; 11 | end 12 | function set.Value(node,newValue) 13 | if isempty(node.Owner) 14 | node.Value = newValue; 15 | else 16 | list = node.Owner; 17 | list.remove(node); 18 | node.Value = newValue; 19 | list.insert(node) 20 | end 21 | end 22 | function res = gt(node1,node2) % > 23 | res = node1.Value > node2.Value; 24 | end 25 | function res = ge(node1,node2) % >= 26 | res = node1.Value >= node2.Value; 27 | end 28 | function res = lt(node1,node2) % < 29 | res = node1.Value < node2.Value; 30 | end 31 | function res = le(node1,node2) % <= 32 | res = node1.Value <= node2.Value; 33 | end 34 | function res = eq(node1,node2) % == 35 | res = node1.Value == node2.Value; 36 | end 37 | function res = ne(node1,node2) % ~= 38 | res = node1.Value ~= node2.Value; 39 | end 40 | function disp(node) 41 | disp(node.Value); 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/SortedNumber_v1.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber_v1 < LinkedNode % SortedNumber_v1 2 | properties 3 | Value 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | if ~isscalar(n) || ~isnumeric(n) 8 | error('Expected numeric scalar Value'); 9 | end 10 | node.Value = n; 11 | end 12 | function res = gt(node1,node2) 13 | res = node1.Value > node2.Value; 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/SortedNumber_v2.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v2 2 | properties 3 | Value 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n (1,1) {mustBeNumeric} = 0 9 | end 10 | node.Value = n; 11 | end 12 | function res = gt(node1,node2) 13 | res = node1.Value > node2.Value; 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/SortedNumber_v3.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v3 2 | properties 3 | Value 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n (1,1) {mustBeNumeric} = 0 9 | end 10 | node.Value = n; 11 | end 12 | function res = gt(node1,node2) % > 13 | res = node1.Value > node2.Value; 14 | end 15 | function res = ge(node1,node2) % >= 16 | res = node1.Value >= node2.Value; 17 | end 18 | function res = lt(node1,node2) % < 19 | res = node1.Value < node2.Value; 20 | end 21 | function res = le(node1,node2) % <= 22 | res = node1.Value <= node2.Value; 23 | end 24 | function res = eq(node1,node2) % == 25 | res = node1.Value == node2.Value; 26 | end 27 | function res = ne(node1,node2) % ~= 28 | res = node1.Value ~= node2.Value; 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/SortedNumber_v4.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v4 2 | properties 3 | Value 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n (1,1) {mustBeNumeric} = 0 9 | end 10 | node.Value = n; 11 | end 12 | function set.Value(node,newValue) 13 | arguments 14 | node 15 | newValue (1,1) {mustBeNumeric} 16 | end 17 | if isempty(node.Owner) 18 | node.Value = newValue; 19 | else 20 | list = node.Owner; 21 | list.remove(node); 22 | node.Value = newValue; 23 | list.insert(node) 24 | end 25 | end 26 | function res = gt(node1,node2) % > 27 | res = node1.Value > node2.Value; 28 | end 29 | function res = ge(node1,node2) % >= 30 | res = node1.Value >= node2.Value; 31 | end 32 | function res = lt(node1,node2) % < 33 | res = node1.Value < node2.Value; 34 | end 35 | function res = le(node1,node2) % <= 36 | res = node1.Value <= node2.Value; 37 | end 38 | function res = eq(node1,node2) % == 39 | res = node1.Value == node2.Value; 40 | end 41 | function res = ne(node1,node2) % ~= 42 | res = node1.Value ~= node2.Value; 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/SortedNumber_v5.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v5 2 | properties 3 | Value 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n (1,1) {mustBeNumeric} = 0 9 | end 10 | node.Value = n; 11 | end 12 | function set.Value(node,newValue) 13 | arguments 14 | node 15 | newValue (1,1) {mustBeNumeric} 16 | end 17 | if isempty(node.Owner) 18 | node.Value = newValue; 19 | else 20 | list = node.Owner; 21 | list.remove(node); 22 | node.Value = newValue; 23 | list.insert(node) 24 | end 25 | end 26 | function res = gt(node1,node2) % > 27 | res = node1.Value > node2.Value; 28 | end 29 | function res = ge(node1,node2) % >= 30 | res = node1.Value >= node2.Value; 31 | end 32 | function res = lt(node1,node2) % < 33 | res = node1.Value < node2.Value; 34 | end 35 | function res = le(node1,node2) % <= 36 | res = node1.Value <= node2.Value; 37 | end 38 | function res = eq(node1,node2) % == 39 | res = node1.Value == node2.Value; 40 | end 41 | function res = ne(node1,node2) % ~= 42 | res = node1.Value ~= node2.Value; 43 | end 44 | function disp(node) 45 | disp(node.Value); 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/SortedNumber_v6.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v6 2 | properties 3 | Value (1,1) {mustBeNumeric} = 0 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n = 0 9 | end 10 | node.Value = n; 11 | end 12 | function set.Value(node,newValue) 13 | if isempty(node.Owner) 14 | node.Value = newValue; 15 | else 16 | list = node.Owner; 17 | list.remove(node); 18 | node.Value = newValue; 19 | list.insert(node) 20 | end 21 | end 22 | function res = gt(node1,node2) % > 23 | res = node1.Value > node2.Value; 24 | end 25 | function res = ge(node1,node2) % >= 26 | res = node1.Value >= node2.Value; 27 | end 28 | function res = lt(node1,node2) % < 29 | res = node1.Value < node2.Value; 30 | end 31 | function res = le(node1,node2) % <= 32 | res = node1.Value <= node2.Value; 33 | end 34 | function res = eq(node1,node2) % == 35 | res = node1.Value == node2.Value; 36 | end 37 | function res = ne(node1,node2) % ~= 38 | res = node1.Value ~= node2.Value; 39 | end 40 | function disp(node) 41 | disp(node.Value); 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_1.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_1.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_2.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_2.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_3.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_3.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_4.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_4.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_5.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_5.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_6.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_6.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_7.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_7.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_8.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/13-More_on_OOP/live_script_5_3_8.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/BusinessContact.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact % BusinessContact_v8 2 | properties 3 | Company (1,1) string 4 | Fax (1,1) string 5 | end 6 | methods (Access = protected) 7 | function name = nameToCompare(obj) 8 | name = upper(append(obj.Company, " ", obj.LastName, " ", obj.FirstName)); 9 | end 10 | end 11 | methods 12 | function obj = BusinessContact(cname,lname,fname,phone,f) 13 | arguments 14 | cname = "" 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | f = "" 19 | end 20 | obj@Contact(lname,fname,phone); 21 | obj.Company = cname; 22 | obj.Fax = f; 23 | end 24 | function set.Company(obj,cname) 25 | obj.Company = cname; 26 | obj.reposition(); 27 | end 28 | function set.Fax(obj,f) 29 | obj.Fax = f; 30 | end 31 | function disp(node) 32 | fprintf(' Company: %s\n',node.Company); 33 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 34 | fprintf(' Tel: %s\n',node.PhoneNumber); 35 | fprintf(' Fax: %s\n\n',node.Fax); 36 | end 37 | end 38 | 39 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/BusinessContact_v5.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact % BusinessContact_v5 2 | properties 3 | Company 4 | Fax 5 | end 6 | methods 7 | function obj = BusinessContact(cname,lname,fname,phone,f) 8 | if nargin < 5 f = ""; end 9 | if nargin < 4 phone = ""; end 10 | if nargin < 3 fname = ""; end 11 | if nargin < 2 lname = ""; end 12 | if nargin < 1 cname = ""; end 13 | obj@Contact(lname,fname,phone); 14 | obj.Company = string(cname); 15 | obj.Fax = string(f); 16 | end 17 | function obj = set.Company(obj,cname) 18 | obj.Company = string(cname); 19 | end 20 | function obj = set.Fax(obj,f) 21 | obj.Fax = string(f); 22 | end 23 | end 24 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/BusinessContact_v6.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact % BusinessContact_v6 2 | properties 3 | Company (1,1) string 4 | Fax (1,1) string 5 | end 6 | methods 7 | function obj = BusinessContact(cname,lname,fname,phone,f) 8 | arguments 9 | cname = "" 10 | lname = "" 11 | fname = "" 12 | phone = "" 13 | f = "" 14 | end 15 | obj@Contact(lname,fname,phone); 16 | obj.Company = cname; 17 | obj.Fax = f; 18 | end 19 | function set.Company(obj,cname) 20 | obj.Company = cname; 21 | end 22 | function set.Fax(obj,f) 23 | obj.Fax = f; 24 | end 25 | end 26 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/BusinessContact_v7.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact % BusinessContact_v7 2 | properties 3 | Company (1,1) string 4 | Fax (1,1) string 5 | end 6 | methods (Access = protected) 7 | function name = nameToCompare(obj) 8 | name = upper(append(obj.Company, " ", obj.LastName, " ", obj.FirstName)); 9 | end 10 | end 11 | methods 12 | function obj = BusinessContact(cname,lname,fname,phone,f) 13 | arguments 14 | cname = "" 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | f = "" 19 | end 20 | obj@Contact(lname,fname,phone); 21 | obj.Company = cname; 22 | obj.Fax = f; 23 | end 24 | function set.Company(obj,cname) 25 | obj.Company = cname; 26 | end 27 | function set.Fax(obj,f) 28 | obj.Fax = f; 29 | end 30 | function disp(node) 31 | fprintf(' Company: %s\n',node.Company); 32 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 33 | fprintf(' Tel: %s\n',node.PhoneNumber); 34 | fprintf(' Fax: %s\n\n',node.Fax); 35 | end 36 | end 37 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/BusinessContact_v8.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact % BusinessContact_v8 2 | properties 3 | Company (1,1) string 4 | Fax (1,1) string 5 | end 6 | methods (Access = protected) 7 | function name = nameToCompare(obj) 8 | name = upper(append(obj.Company, " ", obj.LastName, " ", obj.FirstName)); 9 | end 10 | end 11 | methods 12 | function obj = BusinessContact(cname,lname,fname,phone,f) 13 | arguments 14 | cname = "" 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | f = "" 19 | end 20 | obj@Contact(lname,fname,phone); 21 | obj.Company = cname; 22 | obj.Fax = f; 23 | end 24 | function set.Company(obj,cname) 25 | obj.Company = cname; 26 | object.reposition(); 27 | end 28 | function set.Fax(obj,f) 29 | obj.Fax = f; 30 | end 31 | function disp(node) 32 | fprintf(' Company: %s\n',node.Company); 33 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 34 | fprintf(' Tel: %s\n',node.PhoneNumber); 35 | fprintf(' Fax: %s\n\n',node.Fax); 36 | end 37 | end 38 | 39 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/Contact.m: -------------------------------------------------------------------------------- 1 | classdef Contact < LinkedNode % Contact_v9 2 | properties 3 | FirstName (1,1) string 4 | LastName (1,1) string 5 | PhoneNumber (1,1) string 6 | end 7 | methods (Access = protected) 8 | function name = nameToCompare(obj) 9 | name = upper(append(obj.LastName, " ", obj.FirstName)); 10 | end 11 | end 12 | methods 13 | function obj = Contact(lname,fname,phone) 14 | arguments 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | end 19 | obj.LastName = lname; 20 | obj.FirstName = fname; 21 | obj.PhoneNumber = phone; 22 | end 23 | function set.LastName(obj,lname) 24 | obj.LastName = lname; 25 | obj.reposition(); 26 | end 27 | function set.FirstName(obj,fname) 28 | obj.FirstName = fname; 29 | obj.reposition(); 30 | end 31 | function set.PhoneNumber(obj,phone) 32 | obj.PhoneNumber = phone; 33 | end 34 | function a = gt(o1,o2) 35 | a = o1.nameToCompare() > o2.nameToCompare(); 36 | end 37 | function a = ge(o1,o2) 38 | a = o1.nameToCompare() >= o2.nameToCompare(); 39 | end 40 | function a = lt(o1,o2) 41 | a = o1.nameToCompare() < o2.nameToCompare(); 42 | end 43 | function a = le(o1,o2) 44 | a = o1.nameToCompare() <= o2.nameToCompare(); 45 | end 46 | function a = eq(o1,o2) 47 | a = o1.nameToCompare() == o2.nameToCompare(); 48 | end 49 | function a = ne(o1,o2) 50 | a = o1.nameToCompare() ~= o2.nameToCompare(); 51 | end 52 | function disp(node) 53 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 54 | fprintf(' Tel: %s\n\n', node.PhoneNumber); 55 | end 56 | end 57 | 58 | end 59 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/Contact_v4.m: -------------------------------------------------------------------------------- 1 | classdef Contact % Contact_v4 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | if nargin < 3, phone = ""; end 10 | if nargin < 2, fname = ""; end 11 | if nargin < 1, lname = ""; end 12 | obj.LastName = string(lname); 13 | obj.FirstName = string(fname); 14 | obj.PhoneNumber = string(phone); 15 | end 16 | function obj = set.LastName(obj,lname) 17 | obj.LastName = string(lname); 18 | end 19 | function obj = set.FirstName(obj,fname) 20 | obj.FirstName = string(fname); 21 | end 22 | function obj = set.PhoneNumber(obj,phone) 23 | obj.PhoneNumber = string(phone); 24 | end 25 | end 26 | end 27 | 28 | 29 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/Contact_v5.m: -------------------------------------------------------------------------------- 1 | classdef Contact < LinkedNode % Contact_v5 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | if nargin < 3, phone = ""; end 10 | if nargin < 2, fname = ""; end 11 | if nargin < 1, lname = ""; end 12 | obj.LastName = string(lname); 13 | obj.FirstName = string(fname); 14 | obj.PhoneNumber = string(phone); 15 | end 16 | function obj = set.LastName(obj,lname) 17 | obj.LastName = string(lname); 18 | end 19 | function obj = set.FirstName(obj,fname) 20 | obj.FirstName = string(fname); 21 | end 22 | function obj = set.PhoneNumber(obj,phone) 23 | obj.PhoneNumber = string(phone); 24 | end 25 | end 26 | end 27 | 28 | 29 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/Contact_v6.m: -------------------------------------------------------------------------------- 1 | classdef Contact < LinkedNode % Contact_v6 2 | properties 3 | FirstName (1,1) string 4 | LastName (1,1) string 5 | PhoneNumber (1,1) string 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | arguments 10 | lname = "" 11 | fname = "" 12 | phone = "" 13 | end 14 | obj.LastName = lname; 15 | obj.FirstName = fname; 16 | obj.PhoneNumber = phone; 17 | end 18 | function set.LastName(obj,lname) 19 | obj.LastName = lname; 20 | end 21 | function set.FirstName(obj,fname) 22 | obj.FirstName = fname; 23 | end 24 | function set.PhoneNumber(obj,phone) 25 | obj.PhoneNumber = phone; 26 | end 27 | end 28 | end 29 | 30 | 31 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/Contact_v7.m: -------------------------------------------------------------------------------- 1 | classdef Contact < LinkedNode % Contact_v7 2 | properties 3 | FirstName (1,1) string 4 | LastName (1,1) string 5 | PhoneNumber (1,1) string 6 | end 7 | methods 8 | function obj = Contact(lname,fname,phone) 9 | arguments 10 | lname = "" 11 | fname = "" 12 | phone = "" 13 | end 14 | obj.LastName = lname; 15 | obj.FirstName = fname; 16 | obj.PhoneNumber = phone; 17 | end 18 | function set.LastName(obj,lname) 19 | obj.LastName = lname; 20 | end 21 | function set.FirstName(obj,fname) 22 | obj.FirstName = fname; 23 | end 24 | function set.PhoneNumber(obj,phone) 25 | obj.PhoneNumber = phone; 26 | end 27 | function disp(node) 28 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 29 | fprintf(' Tel: %s\n\n', node.PhoneNumber); 30 | end 31 | end 32 | end 33 | 34 | 35 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/Contact_v8.m: -------------------------------------------------------------------------------- 1 | classdef Contact < LinkedNode % Contact_v8 2 | properties 3 | FirstName (1,1) string 4 | LastName (1,1) string 5 | PhoneNumber (1,1) string 6 | end 7 | methods (Access = protected) 8 | function name = nameToCompare(obj) 9 | name = upper(append(obj.LastName, " ", obj.FirstName)); 10 | end 11 | end 12 | methods 13 | function obj = Contact(lname,fname,phone) 14 | arguments 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | end 19 | obj.LastName = lname; 20 | obj.FirstName = fname; 21 | obj.PhoneNumber = phone; 22 | end 23 | function set.LastName(obj,lname) 24 | obj.LastName = lname; 25 | end 26 | function set.FirstName(obj,fname) 27 | obj.FirstName = fname; 28 | end 29 | function set.PhoneNumber(obj,phone) 30 | obj.PhoneNumber = phone; 31 | end 32 | function a = gt(o1,o2) 33 | a = o1.nameToCompare() > o2.nameToCompare(); 34 | end 35 | function a = ge(o1,o2) 36 | a = o1.nameToCompare() >= o2.nameToCompare(); 37 | end 38 | function a = lt(o1,o2) 39 | a = o1.nameToCompare() < o2.nameToCompare(); 40 | end 41 | function a = le(o1,o2) 42 | a = o1.nameToCompare() <= o2.nameToCompare(); 43 | end 44 | function a = eq(o1,o2) 45 | a = o1.nameToCompare() == o2.nameToCompare(); 46 | end 47 | function a = ne(o1,o2) 48 | a = o1.nameToCompare() ~= o2.nameToCompare(); 49 | end 50 | function disp(node) 51 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 52 | fprintf(' Tel: %s\n\n', node.PhoneNumber); 53 | end 54 | end 55 | 56 | end 57 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/Contact_v9.m: -------------------------------------------------------------------------------- 1 | classdef Contact < LinkedNode % Contact_v9 2 | properties 3 | FirstName (1,1) string 4 | LastName (1,1) string 5 | PhoneNumber (1,1) string 6 | end 7 | methods (Access = protected) 8 | function name = nameToCompare(obj) 9 | name = upper(append(obj.LastName, " ", obj.FirstName)); 10 | end 11 | end 12 | methods 13 | function obj = Contact(lname,fname,phone) 14 | arguments 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | end 19 | obj.LastName = lname; 20 | obj.FirstName = fname; 21 | obj.PhoneNumber = phone; 22 | end 23 | function set.LastName(obj,lname) 24 | obj.LastName = lname; 25 | obj.reposition(); 26 | end 27 | function set.FirstName(obj,fname) 28 | obj.FirstName = fname; 29 | obj.reposition(); 30 | end 31 | function set.PhoneNumber(obj,phone) 32 | obj.PhoneNumber = phone; 33 | end 34 | function a = gt(o1,o2) 35 | a = o1.nameToCompare() > o2.nameToCompare(); 36 | end 37 | function a = ge(o1,o2) 38 | a = o1.nameToCompare() >= o2.nameToCompare(); 39 | end 40 | function a = lt(o1,o2) 41 | a = o1.nameToCompare() < o2.nameToCompare(); 42 | end 43 | function a = le(o1,o2) 44 | a = o1.nameToCompare() <= o2.nameToCompare(); 45 | end 46 | function a = eq(o1,o2) 47 | a = o1.nameToCompare() == o2.nameToCompare(); 48 | end 49 | function a = ne(o1,o2) 50 | a = o1.nameToCompare() ~= o2.nameToCompare(); 51 | end 52 | function disp(node) 53 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 54 | fprintf(' Tel: %s\n\n', node.PhoneNumber); 55 | end 56 | end 57 | 58 | end 59 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/DList.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v6 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function disp(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/DList_6.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v6 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function disp(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/LinkedNode.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v9 2 | properties (Access = {?DList ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | disp(a) 15 | end 16 | methods 17 | function node = LinkedNode() 18 | node.Prev = []; 19 | node.Next = []; 20 | node.Owner = []; 21 | end 22 | function delete(node) 23 | if ~isempty(node.Owner) 24 | node.Owner.remove(node); 25 | end 26 | end 27 | end 28 | methods (Access = protected) 29 | function reposition(obj) 30 | if ~isempty(obj.Owner) 31 | list = obj.Owner; 32 | list.remove(obj); 33 | list.insert(obj); 34 | end 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/LinkedNode_v8.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v8 2 | properties (Access = {?DList ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | disp(a) 15 | end 16 | methods 17 | function node = LinkedNode() 18 | node.Prev = []; 19 | node.Next = []; 20 | node.Owner = []; 21 | end 22 | function delete(node) 23 | if ~isempty(node.Owner) 24 | node.Owner.remove(node); 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/LinkedNode_v9.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v9 2 | properties (Access = {?DList ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | disp(a) 15 | end 16 | methods 17 | function node = LinkedNode() 18 | node.Prev = []; 19 | node.Next = []; 20 | node.Owner = []; 21 | end 22 | function delete(node) 23 | if ~isempty(node.Owner) 24 | node.Owner.remove(node); 25 | end 26 | end 27 | end 28 | methods (Access = protected) 29 | function reposition(obj) 30 | if ~isempty(obj.Owner) 31 | list = obj.Owner; 32 | list.remove(obj); 33 | list.insert(obj); 34 | end 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/OrderedList.m: -------------------------------------------------------------------------------- 1 | classdef OrderedList < DList % OrderedList_v2 2 | methods 3 | function insert(list,node) 4 | if ~isa(node,'Contact') && ~isa(node,'BusinessContact') 5 | fprintf('Must be Contact or BusinessContact\n'); 6 | return 7 | end 8 | if ~isempty(node.Owner) 9 | if node.Owner ~= list 10 | node.Owner.remove(node); 11 | else 12 | return; 13 | end 14 | end 15 | node.Owner = list; 16 | list.Length = list.Length + 1; 17 | if isempty(list.Head) 18 | list.Head = node; 19 | list.Tail = node; 20 | node.Prev = []; 21 | node.Next = []; 22 | else 23 | cur = list.Head; 24 | prev = []; 25 | while ~isempty(cur) && node > cur 26 | prev = cur; 27 | cur = cur.Next; 28 | end 29 | if isempty(prev) 30 | node.Next = list.Head; 31 | node.Prev = []; 32 | list.Head.Prev = node; 33 | list.Head = node; 34 | else 35 | prev.Next = node; 36 | node.Prev = prev; 37 | node.Next = cur; 38 | if isempty(cur) 39 | list.Tail = node; 40 | else 41 | cur.Prev = node; 42 | end 43 | end 44 | end 45 | end 46 | function disp(list) 47 | disp('OrderedList containing:'); 48 | item = list.Head; 49 | while ~isempty(item) 50 | item.disp(); 51 | item = item.Next; 52 | end 53 | end 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/OrderedList_v2.m: -------------------------------------------------------------------------------- 1 | classdef OrderedList < DList % OrderedList_v2 2 | methods 3 | function insert(list,node) 4 | if ~isempty(node.Owner) 5 | if node.Owner ~= list 6 | node.Owner.remove(node); 7 | else 8 | return; 9 | end 10 | end 11 | node.Owner = list; 12 | list.Length = list.Length + 1; 13 | if isempty(list.Head) 14 | list.Head = node; 15 | list.Tail = node; 16 | node.Prev = []; 17 | node.Next = []; 18 | else 19 | cur = list.Head; 20 | prev = []; 21 | while ~isempty(cur) && node > cur 22 | prev = cur; 23 | cur = cur.Next; 24 | end 25 | if isempty(prev) 26 | node.Next = list.Head; 27 | node.Prev = []; 28 | list.Head.Prev = node; 29 | list.Head = node; 30 | else 31 | prev.Next = node; 32 | node.Prev = prev; 33 | node.Next = cur; 34 | if isempty(cur) 35 | list.Tail = node; 36 | else 37 | cur.Prev = node; 38 | end 39 | end 40 | end 41 | end 42 | function disp(list) 43 | disp('OrderedList containing:'); 44 | item = list.Head; 45 | while ~isempty(item) 46 | item.disp(); 47 | item = item.Next; 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/SortedNumber.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v6 2 | properties 3 | Value (1,1) {mustBeNumeric} = 0 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n = 0 9 | end 10 | node.Value = n; 11 | end 12 | function node = set.Value(node,newValue) 13 | if isempty(node.Owner) 14 | node.Value = newValue; 15 | else 16 | list = node.Owner; 17 | list.remove(node); 18 | node.Value = newValue; 19 | list.insert(node) 20 | end 21 | end 22 | function res = gt(node1,node2) % > 23 | res = node1.Value > node2.Value; 24 | end 25 | function res = ge(node1,node2) % >= 26 | res = node1.Value >= node2.Value; 27 | end 28 | function res = lt(node1,node2) % < 29 | res = node1.Value < node2.Value; 30 | end 31 | function res = le(node1,node2) % <= 32 | res = node1.Value <= node2.Value; 33 | end 34 | function res = eq(node1,node2) % == 35 | res = node1.Value == node2.Value; 36 | end 37 | function res = ne(node1,node2) % ~= 38 | res = node1.Value ~= node2.Value; 39 | end 40 | function disp(node) 41 | disp(node.Value); 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_1.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_1.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_2.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_2.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_3.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_3.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_4.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/14-Tying_it_All_Together/live_script_5_4_4.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/meld.m: -------------------------------------------------------------------------------- 1 | function out = meld(in1,in2) 2 | % MELD(X,Y) combine inputs 3 | if nargin == 1 4 | out = in1; 5 | elseif isa(in1,"char") && isa(in2,"char") 6 | out = append(in1,in2); 7 | else 8 | if ~isa(in1,"numeric") 9 | in1 = string(in1); 10 | end 11 | if ~isnumeric(in2) % relative of isa() 12 | in2 = string(in2); 13 | end 14 | out = in1 + in2; 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/14-Tying_it_All_Together/scratch.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week5-Object_Oriented_Programming/14-Tying_it_All_Together/scratch.mlx -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/15-Final_Versions/BusinessContact.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact % BusinessContact_v8 2 | properties 3 | Company (1,1) string 4 | Fax (1,1) string 5 | end 6 | methods (Access = protected) 7 | function name = nameToCompare(obj) 8 | name = upper(append(obj.Company, " ", obj.LastName, " ", obj.FirstName)); 9 | end 10 | end 11 | methods 12 | function obj = BusinessContact(cname,lname,fname,phone,f) 13 | arguments 14 | cname = "" 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | f = "" 19 | end 20 | obj@Contact(lname,fname,phone); 21 | obj.Company = cname; 22 | obj.Fax = f; 23 | end 24 | function set.Company(obj,cname) 25 | obj.Company = cname; 26 | obj.reposition(); 27 | end 28 | function set.Fax(obj,f) 29 | obj.Fax = f; 30 | end 31 | function disp(node) 32 | fprintf(' Company: %s\n',node.Company); 33 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 34 | fprintf(' Tel: %s\n',node.PhoneNumber); 35 | fprintf(' Fax: %s\n\n',node.Fax); 36 | end 37 | end 38 | end -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/15-Final_Versions/Contact.m: -------------------------------------------------------------------------------- 1 | classdef Contact < LinkedNode % Contact_v9 2 | properties 3 | FirstName (1,1) string 4 | LastName (1,1) string 5 | PhoneNumber (1,1) string 6 | end 7 | methods (Access = protected) 8 | function name = nameToCompare(obj) 9 | name = upper(append(obj.LastName, " ", obj.FirstName)); 10 | end 11 | end 12 | methods 13 | function obj = Contact(lname,fname,phone) 14 | arguments 15 | lname = "" 16 | fname = "" 17 | phone = "" 18 | end 19 | obj.LastName = lname; 20 | obj.FirstName = fname; 21 | obj.PhoneNumber = phone; 22 | end 23 | function set.LastName(obj,lname) 24 | obj.LastName = lname; 25 | obj.reposition(); 26 | end 27 | function set.FirstName(obj,fname) 28 | obj.FirstName = fname; 29 | obj.reposition(); 30 | end 31 | function set.PhoneNumber(obj,phone) 32 | obj.PhoneNumber = phone; 33 | end 34 | function a = gt(o1,o2) 35 | a = o1.nameToCompare() > o2.nameToCompare(); 36 | end 37 | function a = ge(o1,o2) 38 | a = o1.nameToCompare() >= o2.nameToCompare(); 39 | end 40 | function a = lt(o1,o2) 41 | a = o1.nameToCompare() < o2.nameToCompare(); 42 | end 43 | function a = le(o1,o2) 44 | a = o1.nameToCompare() <= o2.nameToCompare(); 45 | end 46 | function a = eq(o1,o2) 47 | a = o1.nameToCompare() == o2.nameToCompare(); 48 | end 49 | function a = ne(o1,o2) 50 | a = o1.nameToCompare() ~= o2.nameToCompare(); 51 | end 52 | function disp(node) 53 | fprintf(' Name: %s %s\n',node.FirstName,node.LastName); 54 | fprintf(' Tel: %s\n\n', node.PhoneNumber); 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/15-Final_Versions/DList.m: -------------------------------------------------------------------------------- 1 | classdef DList < handle % DList_v6 2 | properties (Access = protected) 3 | Head 4 | Tail 5 | Length 6 | end 7 | methods 8 | function lng = length(list) 9 | lng = list.Length; 10 | end 11 | 12 | function list = DList() 13 | list.Head = []; 14 | list.Tail = []; 15 | list.Length = 0; 16 | end 17 | 18 | function delete(list) 19 | while ~isempty(list.Head) 20 | list.Head.delete(); 21 | end 22 | end 23 | 24 | function insert(list,node) 25 | if ~isempty(node.Owner) 26 | if node.Owner ~= list % New node is in another list, 27 | node.Owner.remove(node); % so we need to remove it. 28 | else 29 | return; % New node is already in this list, 30 | end % so do nothing. 31 | end 32 | if list.Length == 0 % If the list is empty, 33 | list.Head = node; % put new node at the head, 34 | else 35 | list.Tail.Next = node; % else, point tail node at it. 36 | end 37 | node.Next = []; % New node is at the end. 38 | node.Prev = list.Tail; % Previous node is old tail node. 39 | list.Tail = node; % Make Tail node point at new node. 40 | list.Length = list.Length + 1; 41 | node.Owner = list; 42 | end % insert 43 | 44 | function remove(list,node) 45 | if isempty(node) || node.Owner ~= list 46 | error('node is not in the list'); 47 | end 48 | if ~isempty(node.Prev) % If a node precedes the current node, 49 | node.Prev.Next = node.Next; % make preceding node point to the node 50 | else % that follows the current node, 51 | list.Head = node.Next; % else make Head point to it. 52 | end 53 | if ~isempty(node.Next) % If a node follows the current node, 54 | node.Next.Prev = node.Prev; % make its prev point to the node that 55 | else % that precedes the current node 56 | list.Tail = node.Prev; % else make previous node be the tail. 57 | end 58 | list.Length = list.Length - 1; 59 | node.Next = []; 60 | node.Prev = []; 61 | node.Owner = []; 62 | end % remove 63 | 64 | function disp(list) 65 | item = list.Head; 66 | while ~isempty(item) 67 | item.disp 68 | item = item.Next; 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/15-Final_Versions/LinkedNode.m: -------------------------------------------------------------------------------- 1 | classdef LinkedNode < handle % LinkedNode_v9 2 | properties (Access = {?DList ?LinkedNode}) 3 | Prev 4 | Next 5 | Owner 6 | end 7 | methods (Abstract) 8 | gt(a,b) 9 | ge(a,b) 10 | lt(a,b) 11 | le(a,b) 12 | eq(a,b) 13 | ne(a,b) 14 | disp(a) 15 | end 16 | methods 17 | function node = LinkedNode() 18 | node.Prev = []; 19 | node.Next = []; 20 | node.Owner = []; 21 | end 22 | function delete(node) 23 | if ~isempty(node.Owner) 24 | node.Owner.remove(node); 25 | end 26 | end 27 | end 28 | methods (Access = protected) 29 | function reposition(obj) 30 | if ~isempty(obj.Owner) 31 | list = obj.Owner; 32 | list.remove(obj); 33 | list.insert(obj); 34 | end 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/15-Final_Versions/OrderedList.m: -------------------------------------------------------------------------------- 1 | classdef OrderedList < DList % OrderedList_v2 2 | methods 3 | function insert(list,node) 4 | if ~isa(node,'Contact') && ~isa(node,'BusinessContact') 5 | fprintf('Must be Contact or BusinessContact\n'); 6 | return 7 | end 8 | if ~isempty(node.Owner) 9 | if node.Owner ~= list 10 | node.Owner.remove(node); 11 | else 12 | return; 13 | end 14 | end 15 | node.Owner = list; 16 | list.Length = list.Length + 1; 17 | if isempty(list.Head) 18 | list.Head = node; 19 | list.Tail = node; 20 | node.Prev = []; 21 | node.Next = []; 22 | else 23 | cur = list.Head; 24 | prev = []; 25 | while ~isempty(cur) && node > cur 26 | prev = cur; 27 | cur = cur.Next; 28 | end 29 | if isempty(prev) 30 | node.Next = list.Head; 31 | node.Prev = []; 32 | list.Head.Prev = node; 33 | list.Head = node; 34 | else 35 | prev.Next = node; 36 | node.Prev = prev; 37 | node.Next = cur; 38 | if isempty(cur) 39 | list.Tail = node; 40 | else 41 | cur.Prev = node; 42 | end 43 | end 44 | end 45 | end 46 | function disp(list) 47 | disp('OrderedList containing:'); 48 | item = list.Head; 49 | while ~isempty(item) 50 | item.disp(); 51 | item = item.Next; 52 | end 53 | end 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /Week5-Object_Oriented_Programming/15-Final_Versions/SortedNumber.m: -------------------------------------------------------------------------------- 1 | classdef SortedNumber < LinkedNode % SortedNumber_v6 2 | properties 3 | Value (1,1) {mustBeNumeric} = 0 4 | end 5 | methods 6 | function node = SortedNumber(n) 7 | arguments 8 | n = 0 9 | end 10 | node.Value = n; 11 | end 12 | function node = set.Value(node,newValue) 13 | if isempty(node.Owner) 14 | node.Value = newValue; 15 | else 16 | list = node.Owner; 17 | list.remove(node); 18 | node.Value = newValue; 19 | list.insert(node) 20 | end 21 | end 22 | function res = gt(node1,node2) % > 23 | res = node1.Value > node2.Value; 24 | end 25 | function res = ge(node1,node2) % >= 26 | res = node1.Value >= node2.Value; 27 | end 28 | function res = lt(node1,node2) % < 29 | res = node1.Value < node2.Value; 30 | end 31 | function res = le(node1,node2) % <= 32 | res = node1.Value <= node2.Value; 33 | end 34 | function res = eq(node1,node2) % == 35 | res = node1.Value == node2.Value; 36 | end 37 | function res = ne(node1,node2) % ~= 38 | res = node1.Value ~= node2.Value; 39 | end 40 | function disp(node) 41 | disp(node.Value); 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /Week6-Applications/GlobalTemp.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week6-Applications/GlobalTemp.mat -------------------------------------------------------------------------------- /Week6-Applications/GlobalTemps.mlapp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week6-Applications/GlobalTemps.mlapp -------------------------------------------------------------------------------- /Week7-Final_Project/.gitattributes: -------------------------------------------------------------------------------- 1 | # Source files 2 | # ============ 3 | *.m text diff=matlab 4 | 5 | # Binary files 6 | # ============ 7 | *.mlapp binary 8 | *.mat binary -------------------------------------------------------------------------------- /Week7-Final_Project/CovidCountryData.m: -------------------------------------------------------------------------------- 1 | classdef CovidCountryData < Statistics 2 | properties (Access = public) 3 | Name char 4 | IndexOfCountryIncludingGlobal double 5 | IndexOfCountryExcludingGlobal double 6 | CountOfCountry double 7 | NumberOfStates double 8 | ListOfStates cell 9 | ListOfStateNames cell 10 | end 11 | methods 12 | function obj = CovidCountryData(covid_data, nameOfCountry, indexOfCountryIncludingGlobal) 13 | obj@Statistics(covid_data, indexOfCountryIncludingGlobal); 14 | obj.Name = nameOfCountry; 15 | listOfCountriesExcludingGlobal = unique(covid_data(2:end,1)); 16 | listOfCountriesIncludingGlobal = covid_data(:,1); 17 | obj.IndexOfCountryIncludingGlobal = indexOfCountryIncludingGlobal; 18 | [~,obj.IndexOfCountryExcludingGlobal] = ismember(nameOfCountry, listOfCountriesExcludingGlobal); 19 | isExisting = contains(listOfCountriesIncludingGlobal, nameOfCountry); 20 | obj.CountOfCountry = sum(isExisting); 21 | if isequal(obj.CountOfCountry, 1) 22 | obj.NumberOfStates = 0; 23 | obj.ListOfStates = {}; 24 | obj.ListOfStateNames = {}; 25 | else 26 | obj.NumberOfStates = obj.CountOfCountry - 1; 27 | for ii = 1 : obj.NumberOfStates 28 | indexOfState = indexOfCountryIncludingGlobal + ii; 29 | nameOfState = covid_data{indexOfState,2}; 30 | obj.ListOfStates{ii} = CovidStateData(covid_data, nameOfState, indexOfState); 31 | obj.ListOfStateNames{ii} = nameOfState; 32 | end 33 | end 34 | end 35 | end 36 | end -------------------------------------------------------------------------------- /Week7-Final_Project/CovidStateData.m: -------------------------------------------------------------------------------- 1 | classdef CovidStateData < Statistics 2 | properties (Access = public) 3 | Name char 4 | IndexOfState double 5 | end 6 | methods 7 | function obj = CovidStateData(covid_data, nameOfState, indexOfState) 8 | obj@Statistics(covid_data, indexOfState); 9 | obj.Name = nameOfState; 10 | obj.IndexOfState = indexOfState; 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /Week7-Final_Project/MyCovid19App.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/MyCovid19App.exe -------------------------------------------------------------------------------- /Week7-Final_Project/MyCovid19App.mlapp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/MyCovid19App.mlapp -------------------------------------------------------------------------------- /Week7-Final_Project/Statistics.m: -------------------------------------------------------------------------------- 1 | classdef Statistics < handle 2 | properties (GetAccess = public) 3 | Dates cell 4 | CumulativeCases double 5 | DailyCases double 6 | CumulativeDeaths double 7 | DailyDeaths double 8 | end 9 | methods 10 | function obj = Statistics(covid_data, index) 11 | obj.Dates = covid_data(1,3:end); 12 | obj.CumulativeCases = zeros(1,length(obj.Dates)); % pre-allocation 13 | obj.DailyCases = zeros(1,length(obj.Dates)); 14 | for i = 1 : length(obj.Dates) 15 | obj.CumulativeCases(i) = covid_data{index,i+2}(1); 16 | obj.CumulativeDeaths(i) = covid_data{index,i+2}(2); 17 | end 18 | obj.DailyCases = [ obj.CumulativeCases(1), diff(obj.CumulativeCases) ]; 19 | obj.DailyCases(obj.DailyCases < 0) = 0; 20 | obj.DailyDeaths = [ obj.CumulativeDeaths(1), diff(obj.CumulativeDeaths) ]; 21 | obj.DailyDeaths(obj.DailyDeaths < 0) = 0; 22 | end 23 | function value = get.DailyCases(obj) 24 | value = obj.DailyCases; 25 | end 26 | function value = get.CumulativeCases(obj) 27 | value = obj.CumulativeCases; 28 | end 29 | function value = get.CumulativeDeaths(obj) 30 | value = obj.CumulativeDeaths; 31 | end 32 | function value = get.DailyDeaths(obj) 33 | value = obj.DailyDeaths; 34 | end 35 | end 36 | end -------------------------------------------------------------------------------- /Week7-Final_Project/covid_data.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/covid_data.mat -------------------------------------------------------------------------------- /Week7-Final_Project/icons/GitHub-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/icons/GitHub-icon.png -------------------------------------------------------------------------------- /Week7-Final_Project/icons/app-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/icons/app-icon.png -------------------------------------------------------------------------------- /Week7-Final_Project/icons/clear-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/icons/clear-icon.png -------------------------------------------------------------------------------- /Week7-Final_Project/icons/default-setting-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/icons/default-setting-icon.png -------------------------------------------------------------------------------- /Week7-Final_Project/icons/email-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/icons/email-icon.png -------------------------------------------------------------------------------- /Week7-Final_Project/icons/paintbrush-icon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/icons/paintbrush-icon.gif -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/1.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/10.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/2.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/3.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/4.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/5.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/6.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/7.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/8.png -------------------------------------------------------------------------------- /Week7-Final_Project/reference-solutions/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huaminghuangtw/Coursera-Mastering-Programming-with-MATLAB/2bebeeecbb5a2ff7550922f005c06719e986eb02/Week7-Final_Project/reference-solutions/9.png --------------------------------------------------------------------------------