├── Final Problems ├── Echo Generator ├── Image Blur └── Saddle Points ├── README.md ├── Week 1 └── test ├── Week 2 ├── MATLAB as a Calculator └── Wrap Up ├── Week 3 ├── Colon Operator ├── Matrix Arithmetic └── Matrix Indexing Practice ├── Week 4 ├── A Simple Function ├── Corner Case └── Taxi Fare ├── Week 5 ├── Matrix Construction └── Minimum and Maximum ├── Week 6 ├── More Practice ├── Practice if statements ├── Variable Number of Input Arguments └── Wrap Up ├── Week 7 ├── Logical Array Practice ├── Practice For Loops ├── Practice While Loops └── Wrap up ├── Week 8 ├── Simple Encryption └── Sparse Matrix └── Week 9 ├── Excel File IO └── Text File IO /Final Problems/Echo Generator: -------------------------------------------------------------------------------- 1 | function output = echo_gen(input, fs, delay, amp) 2 | samples=round(fs*delay); 3 | if samples>0 4 | x=zeros(samples,1); 5 | else 6 | x=[]; 7 | end 8 | y=amp*input; 9 | echo=[x ; y]; 10 | n=[input ; x]; 11 | w = n+echo; 12 | if max(w)>1 13 | output=w/max(w); 14 | else 15 | output=w; 16 | end 17 | -------------------------------------------------------------------------------- /Final Problems/Image Blur: -------------------------------------------------------------------------------- 1 | function output = blur(img,w) 2 | [r c]=size(img); 3 | A=(ones((r+2*w),(c+2*w)))*300; 4 | A(w+1:w+r,w+1:w+c)=img; 5 | S=(A<300).*A; 6 | for i=w+1:w+r 7 | for j=w+1:w+c 8 | dig=0; 9 | output((i-w),(j-w))=(sum(sum(S(i-w:i+w,j-w:j+w)))); 10 | dig=sum(sum(A(i-w:i+w,j-w:j+w)<270)); 11 | output((i-w),(j-w))=output((i-w),(j-w))/dig; 12 | end 13 | end 14 | output=uint8(output); 15 | -------------------------------------------------------------------------------- /Final Problems/Saddle Points: -------------------------------------------------------------------------------- 1 | function indices = saddle(M) 2 | indices=[]; 3 | [row,col]=size(M); 4 | for i=1:col 5 | for j=1:row 6 | minM=min(M(:,i)); 7 | maxM=max(M(j,:)); 8 | if M(j,i)==minM && minM==maxM 9 | indices=[indices;j i]; 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction-to-Programming-with-MATLAB-solutions- 2 | Solutions to the Coursera course - Introduction to Programming with MATLAB 2019 3 | -------------------------------------------------------------------------------- /Week 1/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Week 2/MATLAB as a Calculator: -------------------------------------------------------------------------------- 1 | x=1000+(1000*10/100); 2 | debt=x+(x*10/100) 3 | -------------------------------------------------------------------------------- /Week 2/Wrap Up: -------------------------------------------------------------------------------- 1 | y=0.1/9.58; 2 | hundred=y*3600 3 | x=42.195/((121*60)+39); 4 | marathon=x*3600 5 | -------------------------------------------------------------------------------- /Week 3/Colon Operator: -------------------------------------------------------------------------------- 1 | odds=1:2:100 2 | evens=100:-2:1 3 | -------------------------------------------------------------------------------- /Week 3/Matrix Arithmetic: -------------------------------------------------------------------------------- 1 | A = [1:5; 6:10; 11:15; 16:20]; 2 | x=ones(1,size(A,1)) 3 | y=ones(size(A,2),1) 4 | result=x*A*y 5 | -------------------------------------------------------------------------------- /Week 3/Matrix Indexing Practice: -------------------------------------------------------------------------------- 1 | A = [1:5; 6:10; 11:15; 16:20]; 2 | v=A(:,2) 3 | A(end,:)=0 4 | -------------------------------------------------------------------------------- /Week 4/A Simple Function: -------------------------------------------------------------------------------- 1 | function area=tri_area(b,h) 2 | area=0.5*b*h 3 | -------------------------------------------------------------------------------- /Week 4/Corner Case: -------------------------------------------------------------------------------- 1 | function [top_left top_right bottom_left bottom_right] = corners(A) 2 | top_left=A(1,1) 3 | top_right=A(1,end) 4 | bottom_left=A(end,1) 5 | bottom_right=A(end,end) 6 | -------------------------------------------------------------------------------- /Week 4/Taxi Fare: -------------------------------------------------------------------------------- 1 | function fare=taxi_fare(d,t) 2 | fare=5+(2*(ceil(d)-1))+(0.25*ceil(t)) 3 | -------------------------------------------------------------------------------- /Week 5/Matrix Construction: -------------------------------------------------------------------------------- 1 | function M=trio(n,m) 2 | M=[ones(n,m);2*ones(n,m);3*ones(n,m)] 3 | -------------------------------------------------------------------------------- /Week 5/Minimum and Maximum: -------------------------------------------------------------------------------- 1 | function [mmr mmm] = minimax(M) 2 | mmr=max(M')-min(M') 3 | mmm=max(max(M))-min(min(M)) 4 | -------------------------------------------------------------------------------- /Week 6/More Practice: -------------------------------------------------------------------------------- 1 | function admit = eligible(v,q) 2 | avg=(v+q)/2; 3 | if avg>=92&&v>88&&q>88 4 | admit=1>0 5 | else 6 | admit=0>1 7 | end 8 | -------------------------------------------------------------------------------- /Week 6/Practice if statements: -------------------------------------------------------------------------------- 1 | function out = picker(condition,in1,in2) 2 | if condition>0 3 | out=in1 4 | else 5 | out=in2 6 | end 7 | -------------------------------------------------------------------------------- /Week 6/Variable Number of Input Arguments: -------------------------------------------------------------------------------- 1 | function too_young = under_age(age,limit) 2 | if nargin==1 3 | limit=21; 4 | end 5 | if age0 7 | else 8 | too_young=1<0 9 | end 10 | -------------------------------------------------------------------------------- /Week 6/Wrap Up: -------------------------------------------------------------------------------- 1 | function valid = valid_date(y,m,d) 2 | if ~isscalar(y)||~isscalar(m)||~isscalar(d) 3 | valid=1<0 4 | else 5 | if m>=1&&m<=12 6 | if m==1||m==3||m==5||m==7||m==8||m==10||m==12 7 | if d>=1&&d<=31 8 | valid=1>0 9 | else 10 | valid=1<0 11 | end 12 | elseif m==4||m==6||m==9||m==11 13 | if d>=1&&d<=30 14 | valid=1>0 15 | else 16 | valid=1<0 17 | end 18 | else 19 | if rem(y,100)==0 20 | if rem(y,400)==0 21 | if d>=1&&d<=29 22 | valid=1>0 23 | else 24 | valid=1<0 25 | end 26 | else 27 | if d>=1&&d<=28 28 | valid=1>0 29 | else 30 | valid=1<0 31 | end 32 | end 33 | elseif rem(y,4)==0 34 | if d>=1&&d<=29 35 | valid=1>0 36 | else 37 | valid=1<0 38 | end 39 | else 40 | if d>=1&&d<=28 41 | valid=1>0 42 | else 43 | valid=1<0 44 | end 45 | end 46 | end 47 | else 48 | valid=1<0 49 | end 50 | end 51 | 52 | -------------------------------------------------------------------------------- /Week 7/Logical Array Practice: -------------------------------------------------------------------------------- 1 | function numfreeze = freezing(temp) 2 | numfreeze=sum(temp<32) 3 | -------------------------------------------------------------------------------- /Week 7/Practice For Loops: -------------------------------------------------------------------------------- 1 | function summa= halfsum(A) 2 | total=0; 3 | for i=1:size(A,1) 4 | for j=size(A,2):-1:i 5 | total=total+A(i,j) 6 | end 7 | end 8 | summa=total; 9 | -------------------------------------------------------------------------------- /Week 7/Practice While Loops: -------------------------------------------------------------------------------- 1 | function k = next_prime(n) 2 | prime=n+1; 3 | while isprime(prime)~=1 4 | prime=prime+1; 5 | end 6 | k=prime 7 | -------------------------------------------------------------------------------- /Week 7/Wrap up: -------------------------------------------------------------------------------- 1 | function [summa index] = max_sum(v,n) 2 | maxv=0; 3 | ind=-1; 4 | if n>size(v,2) 5 | summa=0 6 | index=-1 7 | else 8 | for i=1:n 9 | maxv=sum(v(1:n)); 10 | ind=1; 11 | end 12 | for j=2:(size(v,2)-n+1) 13 | total=0; 14 | total=sum(v(j:(j+n-1))); 15 | if total>maxv 16 | maxv=total; 17 | ind=j; 18 | else 19 | continue; 20 | end 21 | end 22 | end 23 | summa=maxv 24 | index=ind 25 | -------------------------------------------------------------------------------- /Week 8/Simple Encryption: -------------------------------------------------------------------------------- 1 | function encoded = caesar(str,n) 2 | strascii=double(str); 3 | temp=strascii+n; 4 | for i=1:numel(temp) 5 | while (temp(i)>126||temp(i)<32) 6 | if temp(i)>126 7 | diff=temp(i)-126; 8 | temp(i)=31+diff; 9 | else 10 | diff=32-temp(i); 11 | temp(i)=127-diff; 12 | end 13 | end 14 | cipher(i)=temp(i); 15 | end 16 | encoded=char(cipher); 17 | -------------------------------------------------------------------------------- /Week 8/Sparse Matrix: -------------------------------------------------------------------------------- 1 | function matrix = sparse2matrix(cellvec) 2 | sizecell=numel(cellvec); 3 | default = cellvec{1,2}*ones(cellvec{1,1}(1,:)); 4 | for i=3:sizecell 5 | row=cellvec{1,i}(1); 6 | col=cellvec{1,i}(2); 7 | val=cellvec{1,i}(3); 8 | default(row,col)=val; 9 | end 10 | matrix=default; 11 | -------------------------------------------------------------------------------- /Week 9/Excel File IO: -------------------------------------------------------------------------------- 1 | function distance = get_distance(city1,city2) 2 | [num,text]=xlsread('Distances.xlsx'); 3 | row=-1; 4 | column=-1; 5 | rowcity=text(1,:); 6 | columncity=text(:,1); 7 | for i=2:numel(rowcity) 8 | if strcmpi(city1,rowcity(i)) 9 | row=i; 10 | else 11 | continue; 12 | end 13 | end 14 | for i=2:numel(columncity) 15 | if strcmpi(city2,columncity(i)) 16 | column=i; 17 | else 18 | continue; 19 | end 20 | end 21 | if row==-1||column==-1 22 | distance=-1; 23 | else 24 | distance=num(row-1,column-1); 25 | end 26 | -------------------------------------------------------------------------------- /Week 9/Text File IO: -------------------------------------------------------------------------------- 1 | function charnum = char_counter(fname,ch) 2 | fid=fopen(fname,'rt'); 3 | if fid<0 4 | charnum=-1; 5 | return; 6 | end 7 | oneline = fgets(fid); 8 | str=[oneline]; 9 | while ischar(oneline) 10 | oneline=fgets(fid); 11 | str=[str oneline]; 12 | end 13 | count = 0; 14 | if (ischar(ch)) 15 | for i=1:numel(str) 16 | if ch==str(i) 17 | count=count+1; 18 | else 19 | continue; 20 | end 21 | end 22 | charnum=count; 23 | else 24 | charnum = -1; 25 | end 26 | --------------------------------------------------------------------------------