MATLAB as a Calculator
MATLAB as a Calculator
debt=1000+2*1000*10/100+10
Lesson 1 Wrap-up
hundred=100/9.58*18/5
marathon=42195/7299*18/5
Matrix Arithmetic
B=[1 1 1 1]
C=[1;1;1;1;1]
result=B*A*C
A Simple Function
function area=tri_area(b,h)
area=0.5*b*h
Corner Case
function [top_left top_right bottom_left bottom_right]=corners(A)
top_left=A(1, 1)
top_right=A(1, end)
bottom_left=A(end,1)
bottom_right=A(end,end)
Taxi Fare
function fare=taxi_fare(d,t)
fare=5+(2*(ceil(d)-1))+(0.25*ceil(t))
mmr=max(M')-min(M')
mmm=max(max(M))-min(min(M))
Matrix Construction
function M=trio(n,m)
Practice if-statements
function out = picker(condition,in1,in2)
if condition>0
out=in1
else
out=in2
end
More Practice
function admit = eligible(v,q)
avg=(v+q)/2;
if avg>=92&&v>88&&q>88
admit=1>0
else
admit=0>1
end
Lesson 5 Wrap-up
function valid = valid_date(y, m, d)
total = 0;
summa = total;
end
Practice while-loops
function k = next_prime(n)
prime = n + 1;
while ~isprime(prime)
prime = prime + 1;
end
k = prime;
end
numfreeze = sum(temp<32)
Lesson 6 Wrap-up
function [summa index]= max_sum(v,n)
maxv=0;
ind=-1;
if n>size(v,2)
summa=0
index=-1
else
for i=1:n
maxv=sum(v(1:n));
ind=1;
end
for j=2:(size(v,2)-n+1)
total=0;
total=sum(v(j:(j+n-1)));
if total>maxv
maxv=total;
ind=j;
else
continue;
end
end
end
summa =maxv
index=ind
Simple Encryption
function encoded = caesar(str, n)
strascii = double(str);
temp = strascii + n;
for i = 1:numel(temp)
while (temp(i) > 126 || temp(i) < 32)
if temp(i) > 126
diff = temp(i) - 126;
temp(i) = 31 + diff;
else
diff = 32 - temp(i);
temp(i) = 127 - diff;
end
end
end
encoded = char(temp);
end
Sparse Matrix
function matrix = sparse2matrix(cellvec)
sizecell = numel(cellvec);
default = cellvec{1, 2} * ones(cellvec{1, 1}(1, :));
for i=3:sizecell
row=cellvec{1,i}(1);
col=cellvec{1,i}(2);
val=cellvec{1,i}(3);
default(row,col)=val;
end
matrix=default;
end
% Extract city names from the first row and first column
rowCityNames = text(2:end, 1);
colCityNames = text(1, 2:end);
Saddle Points
function indices = saddle(M)
[rows, cols] = size(M);
indices = [];
for i = 1:rows
for j = 1:cols
if M(i, j) == max(M(i, :)) && M(i, j) == min(M(:, j))
indices = [indices; i, j];
end
end
end
end
Image blur
function output = blur(img, w)
B = double(img); % Convert input image to double
[m, n] = size(B); % Get the size of the input image
output = zeros(size(B)); % Create an empty array for the output
for i = 1:m
for j = 1:n
% Define the submatrix indices
r1 = max(i - w, 1);
r2 = min(i + w, m);
c1 = max(j - w, 1);
c2 = min(j + w, n);
% Get the submatrix and calculate the mean
submatrix = B(r1:r2, c1:c2);
output(i, j) = mean(submatrix, 'all');
end
end
% Convert the output back to uint8
output = uint8(output);
end
Echo Generator
function output = echo_gen(input, fs, delay, amp)
[r, c] = size(input);
extraEchoTime = round(delay * fs);
echoSignal = zeros(r + extraEchoTime, 1);
addEchoSignal = echoSignal;
for i = 1:r
echoSignal(extraEchoTime + i, 1) = input(i, 1) * amp;
addEchoSignal(i) = input(i);
end
range = abs(addEchoSignal);
maxRange = max(range);
if maxRange > 1
addEchoSignal = addEchoSignal / maxRange;
end
output = addEchoSignal;
end