% Moed 09A bio MatlabBio 76623 20/7/09 % solution by Roie Knaanie % part A clc clear format compact %% 1 C = [1 0 1;0 1 0;1 0 1].*ones(3) B = 2.5 + rand(3) D = (C&B') < ~C*2 % D = % 0 1 0 % 1 0 1 % 0 1 0 %% 2 A = [1 2 3;2 3 4;4 1 3] .* (ones(3).^2) G = A(2:3,1:2)* A(1:2,2:3) % A(2:3,1:2) * A(1:2,2:3) % 2 3 * 2 3 % 4 1 3 4 % % G = % 13 18 % 11 16 %% 3 N = rand(6); x = 14:-2:4 L = eye(2) + length( N'\x') % x = % 14 12 10 8 6 4 % L = % 7 6 % 6 7 %% 4 B = [ zeros(3,2) fliplr(rand(3)+1.5) ] B(1,7) = -5 D = all(B ~= 0) % B = % 0 0 2.2513 1.8404 1.6190 0 -5.0000 % 0 0 1.7551 2.0853 1.9984 0 0 % 0 0 2.0060 1.7238 2.4597 0 0 % D = % 0 0 1 1 1 0 0 %% 5 A = ones(4)-eye(4)*2 m = 1:size(A,2) A(2,:) = A(2,:)+ mod(m,3) % m = % 1 2 3 4 % A = % -1 1 1 1 % 2 1 1 2 % 1 1 -1 1 % 1 1 1 -1 %% 6 v = 0:5 e = (v+1)>mean(v-1) z = mean( find( e>0 )) % v = % 0 1 2 3 4 5 % e = % 0 1 1 1 1 1 % z = % 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PART B - Q1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [D A]= filter_noise( A ) % Moed 09A MatlabBio part B - q1) if nargin<1 clear, clc format compact % A = fix( rand(12,7)*10 ) - 1; A = [0 5 6 2 -1 0 5 6 5 3 8 -3 6 -1 2 1 8 2 5 8 7 7 -2 3 3 6 4 -4 0 3 ] end [N,M] = size(A) ; D = A; % initialization for i= 1:N for j= 1:M if A(i,j) < 0 if i==1 || i==N || j==1 || j==M % found a negative cell on the frame D(i,j) = 0 ; else S = sum( sum( A(i-1:i+1, j-1:j+1) )) - A(i,j) ; % sum of the 8 neighbours of A(i,j) D(i,j) = S / 8; % store the average in D(i,j) end end end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PART B - Q2 %%%%%%%%%% function R = isSymmetric(A) % Moed 09A MatlabBio part B - q2) % check with: M = magic(4); A = M + M' R = true; % initialization [N, M] = size(A); if N ~= M % check if A is square R = false; return; % or: error('A is not square!'); % the return statement is optional here end for i = 1:N for j = i:N if( A(i,j) ~= A(j,i) ) R = false; end end end %{ function R = isSymmetric(A) % much shorter version: [N, M] = size(A); R = all(all(A==A')) & N==M %} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PART C - Q3 %%%%%%%%%% function v = f3(A) % Moed 09A MatlabBio part C - q3) % f3 gets a matrix of integers A as input, % and returns v. What is v? % try A = [ 1 8 0 3 ;2 0 0 -4] [m,n] = size(A); v = zeros(1,n); for k = 1:n c= A(:,k) ~=0 ; sc = sum( c ); v(k) = sc > 0; end %{A = [ 1 8 0 3 ;2 0 0 -4] % A = % 1 8 0 3 % 2 0 0 -4 % v = f3(A) % v = % 1 1 0 1 %} %{ The solution: a) The output v is a vector v = [ 1 1 0 1 ] b) f3 gets a matrix of integers A and returns a vector v of 1/0 . the elment v(k) = 1 if the k column of A contains a nonzero number, v(k) = 0 if the k column of A is all zeros. Similar to the operation of: v = any(A) %}