% Moed 2010 A bio MatlabBio 76623 12/7/2010 %% part A solution clc clear format compact %% 1 A = [1 2 3;6 5 4;7 8 9] ; B = (eye(3)*3) | diag(min(A)) C = fliplr(~B) < diag(diag(A)) %% 2 D = [1 2 3;7 5 3;4 3 2] M = D .* ones(3) Z = [diag(D') diag(D)] * [1 ; 2] %% 3 r = linspace(2,10,5) s = 8:-2:0 t = r .* s u = t + [2 3 4]*[-2 -1 2]' %% 4 a = 2:6 b = [ a>=mean(a) zeros(1,3) a(2:3) ] b(2,8) = -3 x = any(b) %% 5 v = 1:5 f = (v+1) > mean(v - min(v)) g = mean( find( f )) %% sloutions: %{ %% 1 B = 1 0 0 0 1 0 0 0 1 C = 0 0 0 0 1 0 0 0 1 %% 2 D = 1 2 3 7 5 3 4 3 2 M = 1 2 3 7 5 3 4 3 2 Z = 3 15 6 %% 3 r = 2 4 6 8 10 s = 8 6 4 2 0 t = 16 24 24 16 0 u = 17 25 25 17 1 %% 4 a = 2 3 4 5 6 b = 0 0 1 1 1 0 0 0 3 4 b = 0 0 1 1 1 0 0 0 3 4 0 0 0 0 0 0 0 -3 0 0 x = 0 0 1 1 1 0 0 1 1 1 %% 5 v = 1 2 3 4 5 f = 0 1 1 1 1 g = 3.5000 %} ------------------------------------------------------------------------------------- function B = q1( A , k ) % Moed 2010a MatlabBio part B - q1) if nargin<1 clear, clc format compact A = reshape( randperm(16) , 4 , 4) - 8 k = 2 end B=A; [r,c]=size(A); dmax=max(diag(A)); Amean=mean( mean(A) ); % change B using 2 for loops for i=1:r for j=1:c if i==j && A(i,j)<0 B(i,j) = dmax ; end if A(i,j)>0 && mod( A(i,j) , k ) == 0 B(i,j) = Amean ; end end end end ---------------------------------------------------------------------- function L = q2( M , z ) % Moed 2010a MatlabBio part B - q2) if nargin<1 clear, clc format compact M = magic(5) , z = 106 % is true ; z = 120 is false end [r,c]=size(M); count = 0 % count 3x3 squares for i=2:r-1 for j=2:c-1 if sum(sum( M(i-1:i+1 , j-1:j+1))) > z count = count + 1 end end end countall = (r-2)*(c-2); L = count > countall/2; end ----------------------------------------------------- function u = q3(D) % Moed 2010A MatlabBio part C - q3) % q3 gets a matrix of integers D as input, % and returns u. What is u? % try D = [2 12 0 7; 3 6 5 9] [m,n] = size(D); u = zeros(m*n,1); i = 1; for y = 1:n for x = 1:m u(i) = D(x,y); i = i + 1; end end %{ The solution for D = 2 12 0 7 3 6 5 9 u = q3(D) u = 2 3 12 6 0 5 7 9 Similar to the operation of: u = D(:) %}