0% found this document useful (0 votes)
45 views3 pages

Programming Assignments Introduction To Programming With MATLAB Lesson 3

The document contains 8 programming assignments for an introduction to programming with MATLAB course. The assignments include functions to return odd indexed elements of a matrix, create a column vector with integers in descending order, calculate wealth as a percentage based on various coins, convert between distance and time units for light travel, calculate distances between points, return a submatrix from the bottom left of a larger matrix, calculate the mean of squared integers, and create a matrix by raising elements of a vector to successive powers.

Uploaded by

Pandu Ranga
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)
45 views3 pages

Programming Assignments Introduction To Programming With MATLAB Lesson 3

The document contains 8 programming assignments for an introduction to programming with MATLAB course. The assignments include functions to return odd indexed elements of a matrix, create a column vector with integers in descending order, calculate wealth as a percentage based on various coins, convert between distance and time units for light travel, calculate distances between points, return a submatrix from the bottom left of a larger matrix, calculate the mean of squared integers, and create a matrix by raising elements of a vector to successive powers.

Uploaded by

Pandu Ranga
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/ 3

Programming Assignments

Introduction to Programming with MATLAB


Lesson 3
Program 1

function x = odd_index(M)
x = M(1:2:end,1:2:end);

Program 2

function v = int_col(n)
x = [(n-1):-1:1];
y = [n;x'];
if n==2
v=[2;1]
else
temp = y(ceil(end/2),1);
y(ceil(end/2),1) = 1;
y(end,1) = temp;
v=y;
end

Program 3
function tot = rich(A)
tot = [(A(1,1)*1)+(A(1,2)*5)+(A(1,3)*10)+(A(1,4)*25)]/100;

Program 4
function [t,d] = light_time(v)
d = v.*1.609;
t = d./(300000*60);

Program 5

function c = pitty (ab)


x = ab.*ab;
c=sqrt(x*[1;1])

Program 6
function a = bottom_left(A,n)
a = A((end+1-n):end,1:n);

Program 7
function mm = mean_squares(nn)
mm=sum([1:nn].^2)/nn;

Program 8
function H = hulk(v)
x=v';
H=[x x.^2 x.^3];

Score:
Alphanumeric Code:

100
15C359

Solutions to Homework 3
Problem odd_index:
function out = odd_index(M)
out = M(1:2:end, 1:2:end);
end
Problem int_col:
function v = int_col(n)
v = [n 1:n-1]';
end

Note that this is just one possible solution. There are many others.

Problem rich:
function usd = rich(cent)
usd = [0.01 0.05 0.10 0.25] * cent';
end

We use the fact that matrix multiplication sums up a set of products. Multiplying a row vector
with a column vector will result in a scalar. Here it performs the exact calculations we need.

Problem light_time:
function [mins km] = light_time(mile)

end

km = mile * 1.609;
mins = km / 3e5 / 60;

Problem pitty:
function c = pitty(ab)
end

c = sqrt(ab(:,1) .^ 2 + ab(:,2) .^2);

Problem pitty (alternative solution):


function c = pitty(ab)
end

c = sqrt(sum(ab' .^ 2))';

Here we use the fact that the function sum works column by column. So, transposing and then
squaring every element will put the squares of the corresponding a-s and b-s into columns. The
function sum then adds them up, and sqrt computes each element's square root. Finally, we
need to transpose the result back into a column vector.

Problem bottom_left:
function M = bottom_left(N,n)
end

M = N(end-n+1:end, 1:n);

We need the last n rows and the first n columns. The only trick here is that we need end-n+1,
because end-n:end would get us n+1 indexes and not n as required.

Problem mean_squares:
function mm = mean_squares(nn)
end

mm = mean((1:nn).^2);

Problem hulk:
function H = hulk(v)
end

H = [v' (v').^2 (v').^3];

Here we need to remember to transpose the vector to get the required arrangement.

You might also like