Fifth lecture: Problem-solving

1. Write a function called multP(n,m) which takes two integers as input. The output is a matrix, which is the $n\times m$ multiplication table.

function out1=multP(n,m)
v=(1:n)';
w=(1:m);
out1=v*w;

2. Write a function called fibI(n), which takes a positive integer as an input. The output is the first index (i), for which the i. element of the Fibonacci-series is greater then n. (The first and second element of the Fibonacci series is 1, the k. element is the sum of the previous two elements).

function out1=fibI(n)

thisone=0;
nextone=1;

if n<nextone
    out1=1;
    return
end
index=1;
while n>=nextone
    sum1=thisone+nextone;
    thisone=nextone;
    nextone=sum1;
    index=index+1;
end
out1=index;

3. Write a function called thisJanuary(n) which takes a integer between 1 and 31 as input. The function doesn't have an output but displays that which day of the week was the nth of January this year.

function thisJanuary(n)

if mod(n,7)==2 
    fprintf('Monday\n');
elseif mod(n,7)==3 
    fprintf('Tuesday\n');
elseif mod(n,7)==4 
    fprintf('Wednesday\n');
elseif mod(n,7)==5 
    fprintf('Thursday\n');
elseif mod(n,7)==6 
    fprintf('Friday\n');
elseif mod(n,7)==0 
    fprintf('Saturday\n');
else
    fprintf('Sunday\n');
end

4. Write a function called olderOne(year1, month1, day1, year2, month2, day2) which takes six integers as inputs, which are the birthdays of the people. The output is 1 if the first one is older, 2 if the second one and 0 if they have the same age.

function out1=olderOne(year1, month1, day1, year2, month2, day2)

if year1>year2
    out1=2;
elseif year1<year2 
    out1=1;
elseif month1>month2
    out1=2;
elseif month1<month2
    out1=1;
elseif day1>day2
    out1=2;
elseif day1<day2
    out1=1;
else
    out1=0;
end

5. Write a function called swapV(v,a,b), which takes 3 inputs: the v rowvector, and the a and b floating point numbers. The output should be identical to v expect every item, which value equals to a should be changed to b.

function w=swapV(v,a,b)

w=v;
w(w==a)=b;

6. Write a function called intervalMean(v), which takes a vector as an input. The output is the mean of the elements from v, whose value is between 0 and 1.

function ki=intervalMean(v)

ki=mean(v(v>=0 & v<=1));

7. Write a function called histMod(v,a,b), which takes 3 inputs: v is a row vector, and a and b are floating point numbers. The function discards all values from v, whose values are less than a or more than b. After that, it creates a histogram from the remaining items.

function histMod(v,a,b)

k=(v(v=>a & v<=b));
hist(k)

8. Write a function called elliP(a,b), which takes two positive numbers as input. The function plots the parametric curve x(t)=a*cos(t), y(t)=b*sin(t), where the parameter t goes from 0 to 2*pi.

function elliP(a,b)

t=linspace(0,2*pi);
plot(a*cos(t), b*sin(t))
title('Ellipsoid')


9. Write a function called houseP(s), which takes a string as an input. This string is the name of a .csv file, and you may assume, that this file is in our current working directory. The file contains 3 columns, and unknown number of rows, which are data of houses. The price is in the first column (in Forint), the area is in the second (in m^2), the number of bedrooms in the third. Your function has to import the data from this file, and the output is the mean price/m^2 for the houses having exactly 2 bedrooms.

function meanprice=houseP(s)

houses=dlmread(s);
twobedrooms=houses(:,3)==2;
meanprice=mean(houses(twobedrooms,1)./houses(twobedrooms,2));

10.* Write a function called flipP(A), which takes a matrix as an input. The output is a matrix, which we obtained by mirroring A's element to the diagonal going through the positions (1,n) and (n,1).

function B=flipP(A)
n=size(A,1);
B=zeros(size(A));
for i=1:size(A,1)
    for j=1:size(A,2)
            B(n+1-j,n+1-i)=A(i,j);
    end
end