0% found this document useful (0 votes)
12 views

MATLAB as a Calculator

Uploaded by

bnjblock12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

MATLAB as a Calculator

Uploaded by

bnjblock12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

Colon Operator Practice


odds=[1:2:100]
evens=[100:-2:2]

Matrix Indexing Practice


v=A(1:4,2)
A(4,1:5)=0

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))

Minimum and Maximum


function [mmr mmm] = minimax(M)

mmr=max(M')-min(M')
mmm=max(max(M))-min(min(M))
Matrix Construction
function M=trio(n,m)

M=[ones(n,m) ;2*ones(n, m);3*ones(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

Variable Number of Input Arguments


function too_young = under_age(age,limit)
if nargin==1
limit=21;
end
if age<limit too_young=1>0
else
too_young=1<0
end

Lesson 5 Wrap-up
function valid = valid_date(y, m, d)

if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)


valid = false;
else
if m >= 1 && m <= 12
if m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m ==
10 || m == 12
if d >= 1 && d <= 31
valid = true;
else
valid = false;
end
elseif m == 4 || m == 6 || m == 9 || m == 11
if d >= 1 && d <= 30
valid = true;
else
valid = false;
end
else
if rem(y, 100) == 0
if rem(y, 400) == 0
if d >= 1 && d <= 29
valid = true;
else
valid = false;
end
else
if d >= 1 && d <= 28
valid = true;
else
valid = false;
end
end
elseif rem(y, 4) == 0
if d >= 1 && d <= 29
valid = true;
else
valid = false;
end
else
if d >= 1 && d <= 28
valid = true;
else
valid = false;
end
end
end
else
valid = false;
end
end
end
Practice for-loops
function summa = halfsum(A)

total = 0;

% Loop over rows


for i = 1:size(A, 1)
% Loop over columns in reverse order
for j = size(A, 2):-1:i
total = total + A(i, j);
end
end

summa = total;
end

Practice while-loops
function k = next_prime(n)

prime = n + 1;

while ~isprime(prime)
prime = prime + 1;
end

k = prime;
end

Logical Arrays Practice


function numfreeze = freezing(temp)

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

Excel File I/O


function distance = get_distance(city1, city2)
persistent num text

% Load data only if not already loaded


if isempty(num) || isempty(text)
[num, text] = xlsread('Distances.xlsx');
end

% Extract city names from the first row and first column
rowCityNames = text(2:end, 1);
colCityNames = text(1, 2:end);

% Find indices of the specified cities


rowIndex = find(strcmpi(rowCityNames, city1));
colIndex = find(strcmpi(colCityNames, city2));

% Check if one or both cities are not in the file


if isempty(rowIndex) || isempty(colIndex)
distance = -1;
else
% Extract the distance from the corresponding cell in the
matrix
distance = num(rowIndex, colIndex);
end
end

Text File I/O


function charnum = char_counter(fname, character)
% Check if the file exists
if exist(fname, 'file') ~= 2
charnum = -1;
return;
end

% Check if 'character' is a valid char


if ~ischar(character) || numel(character) ~= 1
charnum = -1;
return;
end

% Read the file


try
fileContent = fileread(fname);
catch
charnum = -1;
return;
end

% Count occurrences of the specified character


charnum = sum(fileContent == character);
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

addEchoSignal = addEchoSignal + echoSignal;

range = abs(addEchoSignal);
maxRange = max(range);

if maxRange > 1
addEchoSignal = addEchoSignal / maxRange;
end

output = addEchoSignal;
end

You might also like