├── Books.txt ├── CocaCola.png ├── Course Files.txt ├── Fibonacci Profiler Qsn.jpg ├── Files-Copyrights-to-Akos-Ledeczi ├── Week 4 Efficiency │ ├── Algorithmic Complexity │ │ ├── binary_search.m │ │ ├── fast_flip.m │ │ ├── fibo.m │ │ ├── fibo_last.m │ │ ├── fibo_list.m │ │ ├── fibocnt.m │ │ ├── matmul.m │ │ ├── my_flip.m │ │ ├── my_search.m │ │ ├── test_fast_flip.m │ │ └── test_matmul.m │ ├── Efficiency in Pratice │ │ ├── follows.mat │ │ ├── max_same_follows_v1.m │ │ ├── max_same_follows_v2.m │ │ ├── rooting_v1.m │ │ ├── rooting_v2.m │ │ └── rooting_v3.m │ └── 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 ├── Week2_Functions │ ├── Function Handles │ │ ├── A.m │ │ ├── assignment_rule.m │ │ ├── first_nested_example.m │ │ ├── get_anon_handle.m │ │ └── get_polynomial_handle.m │ ├── Recursion │ │ ├── ifact.m │ │ ├── igcd.m │ │ ├── rfact.m │ │ ├── rgcd.m │ │ ├── sierpinski.m │ │ └── sierpinskiE.m │ └── Variable No of Arguments │ │ ├── distribute.m │ │ ├── find_first.m │ │ ├── print_all.m │ │ └── print_num.m ├── Week3_Potpourri │ ├── Live Scripts │ │ ├── CO2+Mauna+Loa.xlsx │ │ ├── CO2_Atmospheric_Concentrations.mlx │ │ ├── CRNSiteinMaunaLoa.jpg │ │ └── The_Old_Way.m │ └── Mixed Mode │ │ ├── binary_op_examples.mlx │ │ └── matlab.png ├── Week5_OOP │ ├── Final Versions │ │ ├── BusinessContact.m │ │ ├── Contact.m │ │ ├── DList.m │ │ ├── LinkedNode.m │ │ ├── OrderedList.m │ │ └── SortedNumber.m │ ├── 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_5_2_1.mlx │ ├── Introduction │ │ ├── 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 │ ├── 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 │ └── Trying it 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 └── Week6_Applications │ ├── GlobalTemp.mat │ └── GlobalTemps.mlapp ├── GCD_Algorithm_image.jpg ├── How to get Matlab.txt ├── README.md ├── Sierpinski_commands.m ├── Sierpinski_image.jpg ├── Syllabus.txt ├── Week3_practice_quiz.txt ├── digit_sum.m ├── digit_sum_official_solution.m ├── distribute.m ├── edgy.m ├── edgy_official_solution.m ├── electrical_circuits1.jpg ├── electrical_circuits2.jpg ├── fibo_trace.jpg ├── fibo_trace.m ├── fibo_trace_official_solution.m ├── fibor.m ├── fibor_official_solution.m ├── find_first.m ├── grader.m ├── grader_official_solution.m ├── ifact.m ├── lin_reg.m ├── lin_reg_official_solution.m ├── linear_regression.jpg ├── mixit.m ├── mixit_official_solution.m ├── music.mat ├── name_value_pairs.m ├── name_value_pairs_official_solution.m ├── nfact.m ├── palindrome.m ├── palindrome_official_solution.m ├── poly_fun.m ├── poly_fun_official_solution.m ├── print_all.m ├── print_num.m ├── recursive_max.m ├── recursive_max_official_solution.m ├── reversal.m ├── reversal_official_solution.m ├── rgcd.m ├── sierpinski.m ├── sierpinskiEfficient.m ├── voltage.jpg ├── voltage.m ├── voltage_another_solution.m ├── voltage_official_solution.m ├── voters.m ├── voters_official_solution.m ├── x1.mat ├── x2.mat ├── x3.mat ├── x4.mat └── x5.mat /Books.txt: -------------------------------------------------------------------------------- 1 | http://cs103.net/buy(Computer Programming with Matlab by Fitzpatrick and Ledeczi) 2 | Itunes bookstore(https://itunes.apple.com/us/book/computer-programming-matlab/id591606016?mt=13&uo=4) 3 | Textbook 4 | Although the lectures are designed to be self-contained, we recommend that students refer to the eBook, Computer Programming with MATLAB by Fitzpatrick and Ledeczi, which was written specifically for the first two courses of the Specialization. While it does not cover everything the second course does, it serves as a handy reference to the material introduced in the first course of the Specialization. The Apple iBooks version of this book is designed for iPads, iPhones, and MacBooks (OS X Mavericks and above) and is available in 51 countries from the iTunes Bookstore. The price is $10. For readers who prefer Windows, Android, or other platforms, and for readers in countries in which Apple textbooks are not sold, a PDF version is available at http://cs103.net/buy/ for the same price. Note that the company that distributes the PDF version of the book is called Gumroad, so that is what will appear on your credit card or PayPal statement. 5 | 6 | -------------------------------------------------------------------------------- /CocaCola.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/CocaCola.png -------------------------------------------------------------------------------- /Course Files.txt: -------------------------------------------------------------------------------- 1 | https://drive.matlab.com/sharing/99aabc96-9452-45f9-9c5b-29e6ed886994 2 | https://www.mathworks.com/help/matlabdrive/ug/install-matlab-drive.html 3 | Course Files 4 | For your reference and further exploration, a copy of many of the files created in the lectures is provided. To access the files: 5 | 6 | 1. Click this link(https://drive.matlab.com/sharing/99aabc96-9452-45f9-9c5b-29e6ed886994) to access the invitation to the files (you must have a MathWorks account.) 7 | 8 | 2. Accept the invitation to the shared files. If you're using MATLAB Online, your done! Log in to MATLAB Online and the files will be there. 9 | 10 | 3. If you have an installed version of MATLAB, start MATLAB and install MATLAB Drive Connector(https://www.mathworks.com/help/matlabdrive/ug/install-matlab-drive.html) to sync the shared files to your computer. This will create a MATLAB Drive folder on your computer that will sync to MATLAB Drive cloud storage. You can access your files on your computer, in MATLAB Online, or in MATLAB Mobile. 11 | 12 | The files are read-only. You can run them and view the code. If you want to edit the files, make a copy. 13 | -------------------------------------------------------------------------------- /Fibonacci Profiler Qsn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Fibonacci Profiler Qsn.jpg -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Algorithmic Complexity/binary_search.m: -------------------------------------------------------------------------------- 1 | function index = binary_search(v,e,first,last) 2 | if nargin < 3 3 | first = 1; 4 | last = length(v); 5 | end 6 | mid = fix( (first + last)/2 ); 7 | if ~(first <= last) 8 | index = 0; 9 | elseif e == v(mid) 10 | index = mid; 11 | elseif e < v(mid) 12 | index = binary_search(v,e,first, mid-1); 13 | else 14 | index = binary_search(v,e,mid+1, last); 15 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Algorithmic Complexity/fast_flip.m: -------------------------------------------------------------------------------- 1 | 2 | function v = fast_flip(v) 3 | for ii = 1:ceil(length(v)/2) 4 | tmp = v(ii); 5 | v(ii) = v(end-ii+1); 6 | v(end-ii+1) = tmp; 7 | end 8 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Algorithmic Complexity/fibo.m: -------------------------------------------------------------------------------- 1 | function f = fibo(n) 2 | if n <= 2 3 | f = 1; 4 | else 5 | f = fibo(n-2) + fibo(n-1); 6 | end 7 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Algorithmic Complexity/fibo_last.m: -------------------------------------------------------------------------------- 1 | function f = fibo_last(n) 2 | f = fibo_list(n); 3 | f = f(end); 4 | end 5 | 6 | function f = fibo_list(n) 7 | if n <= 2 8 | f = ones(1,n); 9 | else 10 | f = fibo_list(n-1); 11 | f = [f f(end-1)+f(end)]; 12 | end 13 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Algorithmic Complexity/fibo_list.m: -------------------------------------------------------------------------------- 1 | function f = fibo_list(n) 2 | if n <= 2 3 | f = ones(1,n); 4 | else 5 | f = fibo_list(n-1); 6 | f = [f f(end-1)+f(end)]; 7 | end 8 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Algorithmic Complexity/fibocnt.m: -------------------------------------------------------------------------------- 1 | function [f cnt] = fibocnt(n) 2 | persistent count; % must specify persistent 3 | if isempty(count) % first time it is set to [] by MATLAB 4 | count = 1; % so we set it to 1 5 | else 6 | count = count + 1; % subsequent times, we increment by 1 7 | end 8 | if n <= 2 9 | f = 1; 10 | else 11 | f = fibocnt(n-2) + fibocnt(n-1); 12 | end 13 | cnt = count; % a pesistent variable cannot be an output argument 14 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Algorithmic Complexity/matmul.m: -------------------------------------------------------------------------------- 1 | function C = matmul(A,B) 2 | [rowA colA] = size(A); 3 | [rowB colB] = size(B); 4 | if ~ismatrix(A) || ~ismatrix(B) 5 | error('Function matmul works with matrices...'); 6 | elseif colA ~= rowB 7 | error('Inner dimensions must agree!'); 8 | end 9 | 10 | C = zeros(rowA, colB); 11 | for ii = 1:rowA 12 | for jj = 1:colB 13 | for kk = 1:colA 14 | C(ii,jj) = C(ii,jj) + A(ii,kk) * B(kk,jj); 15 | end 16 | end 17 | end 18 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Efficiency in Pratice/follows.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Efficiency in Pratice/follows.mat -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Efficiency in Pratice/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Efficiency in Pratice/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Efficiency in Pratice/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Efficiency in Pratice/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Efficiency in Pratice/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week 4 Efficiency/Vectorization/small2zero_v2.m: -------------------------------------------------------------------------------- 1 | function A = small2zero_v2(A,limit) 2 | A(A 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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Live Scripts/CO2+Mauna+Loa.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Live Scripts/CO2+Mauna+Loa.xlsx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Live Scripts/CO2_Atmospheric_Concentrations.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Live Scripts/CO2_Atmospheric_Concentrations.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Live Scripts/CRNSiteinMaunaLoa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Live Scripts/CRNSiteinMaunaLoa.jpg -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/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'); -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Mixed Mode/binary_op_examples.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Mixed Mode/binary_op_examples.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Mixed Mode/matlab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Mixed Mode/matlab.png -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | 39 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | 58 | end 59 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/Akos.m: -------------------------------------------------------------------------------- 1 | classdef Akos < handle 2 | properties 3 | card = 'Jack of Spades' 4 | end 5 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/Mike.m: -------------------------------------------------------------------------------- 1 | classdef Mike 2 | properties 3 | card = 'Jack of Spades' 4 | end 5 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/Try_Akos.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/Try_Akos.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/Try_Mike.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/Try_Mike.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/live_script_5_2_1.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Handle Classes/live_script_5_2_1.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/BusinessContact_v1.m: -------------------------------------------------------------------------------- 1 | classdef BusinessContact < Contact 2 | properties 3 | Company 4 | Fax 5 | end 6 | end -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/Contact_v1.m: -------------------------------------------------------------------------------- 1 | classdef Contact 2 | properties 3 | FirstName 4 | LastName 5 | PhoneNumber 6 | end 7 | end 8 | 9 | 10 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Introduction/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_1.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_1.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_2.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_2.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_3.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_3.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_4.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_4.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_5.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_5.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_6.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_6.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_7.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_7.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_8.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/More on OOP/live_script_5_3_8.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_1.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_1.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_2.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_2.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_3.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_3.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_4.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/live_script_5_4_4.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it 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 | -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/scratch.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week5_OOP/Trying it Together/scratch.mlx -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week6_Applications/GlobalTemp.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week6_Applications/GlobalTemp.mat -------------------------------------------------------------------------------- /Files-Copyrights-to-Akos-Ledeczi/Week6_Applications/GlobalTemps.mlapp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Files-Copyrights-to-Akos-Ledeczi/Week6_Applications/GlobalTemps.mlapp -------------------------------------------------------------------------------- /GCD_Algorithm_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/GCD_Algorithm_image.jpg -------------------------------------------------------------------------------- /How to get Matlab.txt: -------------------------------------------------------------------------------- 1 | How to Get MATLAB 2 | As a result of support from MathWorks, a license to MATLAB Online for the duration of the course is available. MATLAB Online gives you access to MATLAB directly from your browser on a computer with internet access. To get started using MATLAB Online: 3 | 4 | If you do not already have a MathWorks account, create one here: https://www.mathworks.com/mwaccount/register. Be sure to verify your email (check your Junk/Spam folders) before moving on to step 2. 5 | Set up your MathWorks account with MATLAB Online access by signing in using the course license. (https://www.mathworks.com/licensecenter/classroom/adv_matlab_prog) 6 | After using the link in step 2 to login in MATLAB Online for the first time, bookmark matlab.mathworks.com to use MATLAB Online during the course. 7 | MATLAB Online is supported in Chrome, Firefox, and Safari. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mastering-Programming-With-Matlab 2 | ![](Files-Copyrights-to-Akos-Ledeczi/Week3_Potpourri/Mixed%20Mode/matlab.png) 3 | -------------------------------------------------------------------------------- /Sierpinski_commands.m: -------------------------------------------------------------------------------- 1 | t0=cputime; sierpinski(8); cputime-t0 2 | sierpinski(1); for d=1:9; t0=cputime; sierpinski(d); t(d)=cputime-t0; 3 | plot(t); grid on 4 | % don't use the below one it can crash matlab 5 | t0= cputime; sierpinski(12); cputime - t0 % don't try this with your matlab installed version 6 | % You can try instead: 7 | t0= cputime; sierpinski(2); cputime - t0 8 | -------------------------------------------------------------------------------- /Sierpinski_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/Sierpinski_image.jpg -------------------------------------------------------------------------------- /Syllabus.txt: -------------------------------------------------------------------------------- 1 | Syllabus 2 | The course builds on the foundation laid by the first course of the Specialization called “Introduction to Programming with MATLAB.” It covers more advanced programming concepts such as recursion, function handles, algorithm efficiency and others. At the same time, it presents many features that make MATLAB a powerful programming environment for engineering and scientific computing, such as its support for object oriented programming, the new user interface design environment and Live Scripts. 3 | 4 | The first course in this Specialization has been very popular for several years. Many students praised the programming assignments, some of which were definitely challenging. Many others complained about them at the same time. We strongly believe that one can only learn programming by doing it. Simply repeating the programming examples shown in the videos with minor changes and calling them programming assignments would be easy enough to do. However, we view programming assignments primarily not as assessment but as part of the learning process. Once you finish the Specialization and try to solve problems in your own studies or work, they will not be the same as you see in these courses. So, you need to learn to solve new and unfamiliar problems. And that is why the assignments in our courses are not easy. This is especially true for this course. It is called "mastering programming" for a reason. The programming assignments and the final project will challenge you. But they will make you a better programmer. To make it a little easier on you, multiple programming assignments are grouped together and you do not have to solve all of them to pass. 5 | 6 | The course is divided into the following modules: 7 | 8 | Functions Revisited 9 | 10 | Functions may very well be the most important abstraction in computer programming. We learned a lot about them in the first course, but here we'll cover some more advanced concepts such as recursion, function handles and nested functions among others. 11 | 12 | Potpourri 13 | 14 | Quite a few important concepts were left out of the first course due to time constraints. They do not fall neatly into one category, hence, the strange name. They are in no particular order: mixed mode arithmetic, linear equations, and Live Scripts. 15 | 16 | How to Write Efficient Programs 17 | 18 | Our focus throughout these courses has been on how to solve problems with computer programs. We only cared about the correct result. This lesson focuses on the performance of our programs. It gives a little bit of theoretical background on evaluating the efficiency of algorithms, but we spend most of the time on practical advice on how to write programs that are fast enough for our purposes. 19 | 20 | Object Oriented Programming 21 | 22 | Object Oriented Programming (OOP) is a very important concept in computer science. MATLAB has full support for OOP. This lesson provides an introduction to OOP in general and then covers MATLAB's OOP features through an extended example. 23 | 24 | Applications 25 | 26 | The vast majority of computer programs today has Graphical User Interfaces (GUIs). MATLAB has built-in support to help you create modern GUIs for your programs. This lesson will show how to do just that and it will even cover how to make a standalone application that can run even on computers without MATLAB. 27 | 28 | Project 29 | 30 | The course concludes with a project where you will have to write an object-oriented program with a professional GUI. It is a peer review assignment, so part of your task is to grade other students' projects once you submitted your solution. 31 | -------------------------------------------------------------------------------- /Week3_practice_quiz.txt: -------------------------------------------------------------------------------- 1 | 1. 2 | Question 1 3 | How is a Live Script different from a regular script? 4 | Ans:A Live Script combines regular text and MATLAB code. It allows the user to create a nicely formatted document with embedded code and the output of the code such as plots. 5 | 2. 6 | Question 2 7 | What are the parts that make up a Live Script called? 8 | Ans:Sections 9 | 3. 10 | Question 3 11 | What is the file extension of Live Scripts? 12 | Ans:mlx 13 | -------------------------------------------------------------------------------- /digit_sum.m: -------------------------------------------------------------------------------- 1 | %Write a recursive function called digit_sum that computes the sum of the digits of a positive integer provided as an input argument. You do not have to check the 2 | %input. You are not allowed to use loops orstring conversion functions like str2num. As an illustration, the sum of the digits of the number 12345 is 15. Hint: 3 | %Consider what mathematical operation you need to get the last digit of a base 10 number. 4 | 5 | function output = digit_sum(n) 6 | if n<10 7 | output = fix(n); 8 | end 9 | if n>0 10 | output = mod(n,10)+digit_sum(fix(n*0.1)); 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /digit_sum_official_solution.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 -------------------------------------------------------------------------------- /distribute.m: -------------------------------------------------------------------------------- 1 | function varargout = distribute(v) 2 | for ii = 1:length(v) 3 | varargout{ii} = v(ii); 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /edgy.m: -------------------------------------------------------------------------------- 1 | %Automatically detecting edges in images is an important task in image processing. An edge detector takes an input image and generates another image where the edges in the 2 | %original image are highlighted by gray/white colors while other pixels are black. For each 3x3-pixel subset A, of an image, we can calculate the magnitude of the gradient at the 3 | %center pixel of A as a weighted sum of all the values in A.One common technique is called the Sobel operator specified as 4 | %M = sqrt(Sx^2 + Sy^2) 5 | %where Sx and Sy can be defined as, 6 | %Sx = [[-1 0 1] [-2 0 2] [-1 0 1]:A and Sy = [[1 2 1] [0 0 0] [-1 -2 -1]:A 7 | %Note that the colon in linear algebra means double dot product which is different from its use in MATLAB. So the formula above means that each pixelSx and Sy will be a 8 | %weighted sum of the values of the neighbouring pixels in the original image using the weights specified in the 3x3 matrices above. for example, Sx specifies that the current pixel 9 | %and its top and bottom neighbors are not used (weights are zero), while the top right neighbor has a weight of 1 and the top left has weight -1, etc. The final output pixel is then 10 | %the root sum squared Sx and Sy. 11 | %Create a function edgy that takes an original image input, and produces a processed image. Both the input and the output arguments are grayscale images, that is , atrices 12 | %of uint8 values. 13 | %Note that the output has two fewer rows and columns than the input since the pixels in the first and last columns and rows do not have enough neighbors for the required 14 | %computation. 15 | %Image example is there as CocaCola.png 16 | 17 | %%%%%%%%%%%%%%%%%%%% 18 | 19 | function output = edgy(input_image) 20 | input_image = uint8(input_image); 21 | input_image = double(input_image); 22 | filtered_image = zeros(size(input_image)); 23 | 24 | Mx = [-1 0 1; -2 0 2; -1 0 1]; 25 | My = [-1 -2 -1; 0 0 0; 1 2 1]; 26 | 27 | for i = 1:size(input_image, 1) - 2 28 | for j = 1:size(input_image, 2) - 2 29 | Gx = sum(sum(Mx.*input_image(i:i+2, j:j+2))); 30 | Gy = sum(sum(My.*input_image(i:i+2, j:j+2))); 31 | filtered_image(i+1, j+1) = sqrt(Gx.^2 + Gy.^2); 32 | 33 | end 34 | end 35 | filtered_image = filtered_image(2:(end-1), 2:(end-1)); 36 | output = uint8(filtered_image); 37 | 38 | end 39 | -------------------------------------------------------------------------------- /edgy_official_solution.m: -------------------------------------------------------------------------------- 1 | function out = edgy(in) 2 | % Get the size of the input image 3 | [r, c] = size(in); 4 | 5 | % Create an output array that is two rows and columns smaller 6 | out = zeros(r-2,c-2); 7 | 8 | % Use the size of the new array for looping 9 | [r, c] = size(out); 10 | 11 | % Convert to double for doing calculations 12 | in = double(in); 13 | 14 | % Create the horizontal and vertical edge detector filters 15 | ex = [-1 0 1; -2 0 2; -1 0 1]; 16 | ey = [1 2 1; 0 0 0; -1 -2 -1]; 17 | for ii = 1:r 18 | for jj = 1:c 19 | sx = in(ii:ii+2,jj:jj+2) .* ex; 20 | sy = in(ii:ii+2,jj:jj+2) .* ey; 21 | % Calculate the output pixel value 22 | out(ii,jj) = sqrt(sum(sum(sx(:)))^2 + sum(sum(sy(:)))^2); 23 | end 24 | end 25 | % Convert back to uint8 26 | out = uint8(out); 27 | end 28 | -------------------------------------------------------------------------------- /electrical_circuits1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/electrical_circuits1.jpg -------------------------------------------------------------------------------- /electrical_circuits2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/electrical_circuits2.jpg -------------------------------------------------------------------------------- /fibo_trace.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/fibo_trace.jpg -------------------------------------------------------------------------------- /fibo_trace.m: -------------------------------------------------------------------------------- 1 | % 2 | function [f, trace] = fibo_trace(n, trace) 3 | trace = [trace,n]; 4 | if n<=2 5 | f = 1; 6 | else 7 | [f1, trace] = fibo_trace(n-2, trace); 8 | [f2, trace] = fibo_trace(n-1, trace); 9 | f = f1+f2; 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /fibo_trace_official_solution.m: -------------------------------------------------------------------------------- 1 | function [f, v] = fibo_trace(n,v) 2 | v(end+1) = n; 3 | if n == 1 || n == 2 4 | f = 1; 5 | else 6 | [f1, v] = fibo_trace(n-2,v); 7 | [f2, v] = fibo_trace(n-1,v); 8 | f = f1+f2; 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /fibor.m: -------------------------------------------------------------------------------- 1 | %Write a recursive function called fibor that computes the first n elements of the Fibonacci series where n is sole input argument. You are not allowed to use loops. 2 | %F(n) ={ 1 if n<=2 3 | % F(n-1)+F(n-2) otherwise } 4 | %Here are the first few elements: 1 1 2 3 5 8 13 ... 5 | % Don't use fibor() call more than once for efficient functioning. 6 | 7 | function f = fibor(n) 8 | if n==1 9 | f=[1]; 10 | elseif n==2 11 | f=[1 1]; 12 | else 13 | a=fibor(n-1); 14 | f=[a sum(a(end)+a(end-1))]; 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /fibor_official_solution.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 | -------------------------------------------------------------------------------- /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 13 | 14 | -------------------------------------------------------------------------------- /grader.m: -------------------------------------------------------------------------------- 1 | %Here is you chance to write an autograder! Specifically, write a function called grader that tests two functions (one is supposed to be the student's solution and the other the 2 | %reference solution provided by the instructor) by calling them repeatedly with various input arguments and comparing the results. For simplicity, we assume that both functions take 3 | %exactly one input argument. The inputs to the grader function handles followed by a variable number of additional input arguments. The function must call the two 4 | %functions with each of the supplied input arguments one by one. If the results match for all test cases, that is, for each input argument, the grader function returns logical true. 5 | %Otherwise, it returns false. Note that in order to ensure that the comparison works for arrays and not just scalars, you should use the isequal function as opposed to the == 6 | %operator. Here are a few sample runs using built-in functions: 7 | 8 | >>>grader(@sin,@max,0) 9 | ans = 10 | 11 | logical 12 | 13 | 1 14 | >>>grader(@sin,@max,0,1) 15 | ans = 16 | 17 | logical 18 | 19 | 0 20 | >>>grader(@cos,@cos,-pi,0,pi,[0:0.1:1]) 21 | ans = 22 | 23 | logical 24 | 25 | 1 26 | 27 | %%%%%%%%%%%%%%%%%%%%%% 28 | 29 | function pass = grader(fn1,fn2,varargin) 30 | pass = false; 31 | for ii = 1:length(varargin) 32 | if ~isequal(fn1(varargin{ii}),fn2(varargin{ii})) 33 | return; 34 | end 35 | end 36 | pass = true; 37 | end 38 | -------------------------------------------------------------------------------- /grader_official_solution.m: -------------------------------------------------------------------------------- 1 | function pass = grader(fn1,fn2,varargin) 2 | pass = false; 3 | for ii = 1:length(varargin) 4 | if ~isequal(fn1(varargin{ii}),fn2(varargin{ii})) 5 | return; 6 | end 7 | end 8 | pass = true; 9 | end 10 | -------------------------------------------------------------------------------- /ifact.m: -------------------------------------------------------------------------------- 1 | function f=nfact(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 i = 1:n 7 | f = f * i; 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lin_reg.m: -------------------------------------------------------------------------------- 1 | %Given a set of approximate x and y coordinates of points in a plane, determine the best fitting line in the least square sense. Using the standard formula of a line: ax + b = y, 2 | %compute a and b. That is, write a function called lin_reg that takes two row vectors of the same length called x and y as input arguments(containing x and y coordinates of points) 3 | %and returns two scalars, a and b specifying the line, as output arguments. Here is an example run: 4 | >> [a b] = lin_reg([0 2],[0 2]) 5 | a = 6 | 1 7 | b = 8 | 0 9 | %Here is what you should see when you hit the "Run Function" button below: 10 | %Image Link: "https://github.com/sudhamshu091/Mastering-Programming-With-Matlab/blob/master/linear_regression.jpg" 11 | 12 | %Hint: reformulate the problem so that you can use MATLAB's built-in linear equation solving support, i.e., the \ operator. Keep in mind that in our case in the line equation ax + b =y, 13 | %a and b are the unknowns and not x what we usually have in a system of linear equations. So, there is some math and thinking involved! 14 | 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 | 17 | function [a,b] = lin_reg(x,y) 18 | M = [x; ones(1,length(x))]'; 19 | sol = M\y'; 20 | a = sol(1); 21 | b = sol(2); 22 | end 23 | -------------------------------------------------------------------------------- /lin_reg_official_solution.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 7 | -------------------------------------------------------------------------------- /linear_regression.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/linear_regression.jpg -------------------------------------------------------------------------------- /mixit.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 | 5 | function v = mixit(M,N) 6 | if size(M,2) ~= length(N) 7 | v = []; 8 | else 9 | N = N(:); 10 | M = 2*double(M)/(2^16-1)-1; 11 | v = M*N; 12 | if max(abs(v)) > 1; 13 | v = v/max(abs(v)); 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /mixit_official_solution.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 | -------------------------------------------------------------------------------- /music.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/music.mat -------------------------------------------------------------------------------- /name_value_pairs.m: -------------------------------------------------------------------------------- 1 | %Name-Value pairs are frequently used in programming.For our purposes here,a name-value pair consists of a name(a char vector) and a value that can be of any data type. 2 | % Write a function named name_value_pairs that has a variable number of input arguments representing name-value pairs. Naturally, they come in pairs: the first is the name, the next is the value. This means that the function must be called with an even number of actual input arguments. The function returns a single cell array which has exactly two columns:the first column contains the names, while the second column contains the values. If the function is called with no input arguments, or it is called with an odd number of inputs or if a name is not of char type,the function returns an empty cell array.Here is an example run: 3 | db = name_value_pairs('name','John Smith','age',32,'children',{'Joe','Jill'}) 4 | db = 5 | 6 | 3×2 cell array 7 | 8 | {'name' } {'John Smith'} 9 | {'age' } {[ 32]} 10 | {'children'} {1×2 cell } 11 | 12 | function out = name_value_pairs(varargin) 13 | if nargin>0 && rem(nargin, 2) == 0 14 | out = cell(nargin/2, 2); 15 | for ii = 1:nargin 16 | if rem(ii,2) ~= 0 17 | if ischar(varargin{ii}) 18 | out{(ii+1)/2,1} = varargin{ii}; 19 | else 20 | out ={}; 21 | return; 22 | end 23 | else 24 | out{ii/2,2} = varargin{ii}; 25 | end 26 | end 27 | else 28 | out = {}; 29 | end 30 | end 31 | 32 | 33 | -------------------------------------------------------------------------------- /name_value_pairs_official_solution.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 | -------------------------------------------------------------------------------- /nfact.m: -------------------------------------------------------------------------------- 1 | function f=nfact(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*nfact(n-1); 9 | end 10 | end 11 | 12 | 13 | -------------------------------------------------------------------------------- /palindrome.m: -------------------------------------------------------------------------------- 1 | %Write a function called palindrome that takes one input argument, a char vector and recursively determines whether 2 | %that argument is a palindrome. You are not allowed to use loops, not built-in function like strcmp, etc. The 3 | %function returns true or false. Note that to keep things simple, capitalisation, spaces and punctuation all matter. 4 | %See example runs: 5 | %>>palindrome('racecar') 6 | %ans = 7 | % logical 8 | % 1 9 | %>>palindrome('live on timeemit no evil') 10 | %ans = 11 | % logical 12 | % 1 13 | %>>palindrome('live on time, emit no evil') 14 | %ans = 15 | % logical 16 | % 0 17 | %>>palindrome('Live on time emit no evil') 18 | %ans = 19 | % logical 20 | % 0 21 | 22 | function w=palindrome(v) 23 | a=convertStringsToChars(v); 24 | s=strlength(v); 25 | if s==1 26 | w=true; 27 | elseif s==0 28 | w=true; 29 | else 30 | if a(1) ~= a(end) 31 | w=false; 32 | else 33 | w=palindrome(a(end-s+2:s-1)); 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /palindrome_official_solution.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 | -------------------------------------------------------------------------------- /poly_fun.m: -------------------------------------------------------------------------------- 1 | %Remember the example from the video that showed how to return a function handle to a nested function that computed the value of a polynomial? Here it is: 2 | function fh = get_polynomial_handle(p) 3 | function polynomial = poly(x) 4 | polynomial = 0; 5 | for ii = 1:length(p) 6 | polynomial = polynomial + p(ii) .* x.^(ii - 1); 7 | end 8 | end 9 | fh = @poly; 10 | end 11 | 12 | %It takes a vector of coefficients p, defines a function that returns the value of the polynomial given the scalar input x, and returns a function handle to it. Here is an example run: 13 | >>>p = get_polynomial_handle(1:5) 14 | p = 15 | function_handle with value: 16 | @get_polynomial_handle/poly 17 | >>>p(1) 18 | ans = 19 | 15 20 | %Your task is simple: modify the code above so that it does not use any loops. 21 | 22 | %%%%%%%%%%%%%%%%%%%% 23 | 24 | function fh =poly_fun(p) 25 | function polynomial = poly(x) 26 | polynomial = sum(p.*x.^(0:length(p) - 1)); 27 | end 28 | fh = @poly; 29 | end 30 | -------------------------------------------------------------------------------- /poly_fun_official_solution.m: -------------------------------------------------------------------------------- 1 | function fh = poly_fun(p) 2 | function polynomial = poly(x) 3 | polynomial = sum(p .* x.^(0 : length(p)-1)); 4 | end 5 | fh = @poly; 6 | end 7 | -------------------------------------------------------------------------------- /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 6 | -------------------------------------------------------------------------------- /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 29 | -------------------------------------------------------------------------------- /recursive_max.m: -------------------------------------------------------------------------------- 1 | %Write a recursive function called recursive_max that finds the maximum element in a vector. You are not allowed to use loops or any built-in functions. The sole 2 | %output argument is the maximum value in the input vector. Hint: the maximum value of a vector is the larger of its first element and the maximum of the rest of the 3 | %elements 4 | 5 | function mx=recursive_max(v) 6 | if length(v)==1 7 | mx = v; 8 | elseif v(end) < recursive_max(v(1:end-1)) 9 | mx = recursive_max(v(1:end-1)); 10 | else 11 | mx=v(end); 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /recursive_max_official_solution.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 | -------------------------------------------------------------------------------- /reversal.m: -------------------------------------------------------------------------------- 1 | %Write a recursive function called reversal that returns the input vector with its elements reversed. For example 2 | % v = reversal([1 2 3]) 3 | %would set v equal to [3 2 1]. Of course, you can use the MATLAB indexing trick of 4 | % v = v(end:-1:1) 5 | %but that would not be recursive. Don't ruin the fun! 6 | %Hint: one way to do it manually: move the first element to last and reverse the rest of the vector. Try to formulate this algorithm in a recursive fasion. 7 | 8 | function w=reversal(v) 9 | if length(v) == 1 10 | w=v; 11 | else 12 | w=[v(end) reversal(v(1:end-1))]; 13 | end 14 | -------------------------------------------------------------------------------- /reversal_official_solution.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 | -------------------------------------------------------------------------------- /rgcd.m: -------------------------------------------------------------------------------- 1 | function d = rgcd(x,y) 2 | if (~isscalar(x) || ~isscalar(y)|| x ~= fix(x) || y ~= fix(y) || x < 0 || y < 0) 3 | error('x and y must be non-negative integers'); 4 | end 5 | 6 | a=max([x,y]); 7 | b=min([x,y]); 8 | 9 | if b == 0 10 | d = a; 11 | else 12 | d = rgcd(b,rem(a,b)); 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /sierpinskiEfficient.m: -------------------------------------------------------------------------------- 1 | %Efficient code to generate sierpinski triangle 2 | function sierpinskiEfficient(depth) 3 | s=1; c = [0;0]; % s = length of side, c = center 4 | clf; axis([c(1)-s/2,c(1)+s/2,c(2)-s/2,c(2)+s/2],'equal'); 5 | title(sprintf('Sierpinski Triangle with Depth = %d', depth)) 6 | hold on; 7 | plot(1/2*[-1,0,1,-1],sqrt(3)/4*[-1,1,-1,-1],'r--'); 8 | pts=sier(depth, s, c, []); 9 | plot(pts(1,:),pts(2,:),'k-'); 10 | hold off; 11 | end 12 | function pts = sier(d, s, c, pts) 13 | if d == 0 % base case 14 | pts = [pts, c + [[-s,0,s,-s,nan]/2;sqrt(3)*[-s,s,-s,s,nan]/4]]; 15 | else % recursive case 16 | s = s/2; % cuts size in half 17 | h = s*sqrt(3)/2; % height 18 | pts = sier( d-1, s, c - [s;h]/2, pts ); % bottom left 19 | pts = sier( d-1, s, c + [0;h]/2, pts ); % top middle 20 | pts = sier( d-1, s, c + [s;-h]/2, pts ); % bottom right 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /voltage.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/voltage.jpg -------------------------------------------------------------------------------- /voltage.m: -------------------------------------------------------------------------------- 1 | %See the image of a passive electrical circuit below. Write a function called voltage that computes the voltages at junctions A,B and C. The function has two inputs, V for the 2 | %voltage of the supply in volts and R, a vector of the values of the resistors in ohm. in the figure is R(1), that is, the first element of the vector R. In general, RN is R(N). The 3 | %output of the function is a three-element column vector with the voltage levels at junctions A, B and C, respectively. 4 | %To compute the voltage levels, we can use Kirchhoff's first law (https://en.wikipedia.org/wiki/Kirchoff%27s_first_law)that states that the sum of current flowing in and out of a junction must be zero. So, for example, here is the 5 | %junction must be zero. So, for example, here is the equation for junction A: 6 | %(V − A)/R1 − (A − B)/R7 − A/R2 = 0 7 | %The current across a resistor is the voltage difference divided by the resistance, i.e, iN = (Vin − Vout)/RN . You have to be careful that you use the correct sign for inflow and outflow. In 8 | %the above equation, we assumed that A > B , so the current flows out, hence, the negative sign. But if the assumption was wrong,that will still work since A - B will then be 9 | %negative, so overall, it will turn into a positive inflow value. 10 | %You need to write the remaining two equations for junctions B and C and rearrange the equations to get the canonical form so that we can use MATLAB's support for solving linear 11 | %equations. 12 | %Good values to check your function with: 13 | %R1 = 0 means s that A must be at V level. Same for R3 and R5 for B and C, respectively, 14 | %R2 = 0 makes A = 0. Same for R4 and R6 for B and C, respectively. 15 | %If R1/R2 = R3/R4 = R5/R6 then A,B and C will be at the samelevel independent of R7 and R8. 16 | 17 | %Image = "https://github.com/sudhamshu091/Mastering-Programming-With-Matlab/blob/master/voltage.jpg" 18 | 19 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 20 | 21 | function sol = voltage(V,R) 22 | M = [R(2)*R(7) + R(1)*R(2) + R(1)*R(7), -R(1)*R(2), 0; -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); 0, -R(5)*R(6), R(6)*R(8) + R(5)*R(6) + R(5)*R(8)]; 23 | y = V*[R(2)*R(7);R(4)*R(7)*R(8);R(6)*R(8)]; 24 | sol = M\y; 25 | end 26 | -------------------------------------------------------------------------------- /voltage_another_solution.m: -------------------------------------------------------------------------------- 1 | function output = voltage( v, r ) 2 | %Finding out the rows of the resistance matrix one by one 3 | matRow1 = [r(1)+r(2), -r(1), -r(2), 0, 0]; 4 | matRow2 = [-r(1), r(1)+r(3)+r(7), -r(7), -r(3), 0]; 5 | matRow3 = [-r(2), -r(7), r(2)+r(4)+r(7), 0, -r(4)]; 6 | matRow4 = [0, -r(3), 0, r(3)+r(5)+r(8), -r(8)]; 7 | matRow5 = [0, 0, -r(4), -r(8), r(4)+r(6)+r(8)]; 8 | 9 | resMatrix = [matRow1; matRow2; matRow3; matRow4; matRow5]; %resistance matrix 10 | voltageVector = [v; 0; 0; 0; 0]; %Right side of the linear equations 11 | currentVector = resMatrix\voltageVector; %Finding out the current vectors 12 | va = v - r(1)*(currentVector(1) - currentVector(2)); %dont't mess with loop current with current index 13 | vb = v - r(3)*(currentVector(2) - currentVector(4)); %current index = loop number of current + 1 14 | vc = v - r(5)*currentVector(4); %as root loop current is indexed 1 15 | output = [va; vb; vc]; 16 | 17 | end 18 | -------------------------------------------------------------------------------- /voltage_official_solution.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 | 7 | y = V * [R(2)*R(7); R(4)*R(7)*R(8); R(6)*R(8)]; 8 | % Use the backslash operator to solve the system of linear equations 9 | sol = M \ y; 10 | end 11 | -------------------------------------------------------------------------------- /voters.m: -------------------------------------------------------------------------------- 1 | %In a voting center, the voters are required to give their names and ID numbers to the voting staff before going into voting room.Write a function called voters to record the 2 | %incoming voter's information. The function should take an unspecified number of inputs, but the first inputwill be the current database. The rest of the arguments must come in the 3 | %order of name, a string or char array, and ID, an integer or integer valued double. If there is atleast one occurance of no ID number after a name, or the data types are not what's 4 | %required, return the original database. The function should return a struct array containing Name (a string) and ID (a double) fields as shown below. 5 | 6 | >>>database = voters([], 'Brandon', 12356) 7 | database = 8 | 9 | struct with fields: 10 | 11 | Name: "Brandon" 12 | ID: 12356 13 | >>>database = voters(database, "MIke", 9876, 'Akos', 112233) 14 | database = 15 | 16 | 1×3 struct array with fields: 17 | 18 | Name 19 | ID 20 | >>>database 21 | ans = 22 | 23 | struct with fields: 24 | 25 | Name: "Akos" 26 | ID: 112233 27 | However,if there is an illegal call, the database should not change: 28 | >>>database 29 | database = 30 | 31 | 1×3 struct array with fields: 32 | 33 | Name 34 | ID 35 | >>>database = voters(database, 'Student' , 99999, "No ID") 36 | database = 37 | 38 | 1×3 struct array with fields: 39 | 40 | Name 41 | ID 42 | 43 | 44 | %%%%%%%%%%%%%%%%%% 45 | function d = voters(varargin) 46 | d = 0; 47 | if rem(length(varargin),2) == 0 48 | d = varargin{1}; 49 | else 50 | for i = 2:2:length(varargin) - 1 51 | if ~(ischar(varargin{i}) || isstring(varargin{i})) 52 | d = varargin{1};break; 53 | end 54 | if(ischar(varargin{i+1}) ||isstring(varargin{i+1})) 55 | d = varargin{1};break; 56 | end 57 | if ~(fix(varargin{i+1}) == varargin{i+1}) 58 | d = varargin{1};break; 59 | end 60 | end 61 | end 62 | if ~isequal(d, varargin{1}) 63 | database = varargin{1}; 64 | h = length(database) + 1; 65 | g = length(database) + 1; 66 | for j = 2:length(varargin) 67 | if rem(j,2) == 0 68 | database(h).Name = string(varargin{j}); 69 | h = h + 1; 70 | else 71 | database(g).ID = double(varargin{j}); 72 | g = g + 1; 73 | end 74 | end 75 | d = database; 76 | end 77 | end 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /voters_official_solution.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 | -------------------------------------------------------------------------------- /x1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/x1.mat -------------------------------------------------------------------------------- /x2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/x2.mat -------------------------------------------------------------------------------- /x3.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/x3.mat -------------------------------------------------------------------------------- /x4.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/x4.mat -------------------------------------------------------------------------------- /x5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudhamshu091/Mastering-Programming-With-Matlab/8a2ad5adff29e000f7d1275bd92167a924079238/x5.mat --------------------------------------------------------------------------------