CS 100: Section Assignment S10

Solutions



 

% Problem 1
v = [40 10 30 90 40 60];
A = [1 2 3; 14 5 26; 27 18 9 ; 10 1 15];

ave = sum(v)/length(v);
v = v-ave;

A = A
[m,n] = size(A);
for i=1:m
   ave = sum(A(i,:))/n;
   A(i,:) = A(i,:)-ave;
end
A = A

% Problem 2.
x = linspace(0,5,100);
y = (1 + exp(-x).*sin(x))./(2+ sin(x));
plot(x,y)


% Problem 3
v = [90 30 70 30 10 40 80 100 20 50];
u = BigEntries(v)

% Problem 4
figure
n = 10;
x = linspace(0,1,100);
y = x;
plot(x,y)
hold on
for j=2:n
   y = x.^j;
   plot(x,y)
end
hold off


function y = BigEntries(x)
% x is a vector and y is a subvector of the vector x consisting of those
% x-values that are closer to max(x) than to sum(x)/length(x);

ave = sum(x)/length(x);
m = max(x);
z = (m-x) < abs(x-ave);   % z is a 0-1 vector the same size as x. 1's where the inequality is true.
idx = find(z);            % The vector of indices where z is nonzero
y = x(idx);

A =

     1     2     3
    14     5    26
    27    18     9
    10     1    15


A =

   -1.0000         0    1.0000
   -1.0000  -10.0000   11.0000
    9.0000         0   -9.0000
    1.3333   -7.6667    6.3333


u =

    90    80   100