%Lab 6 % % 1 sample solution % function loc = findPattern(dna, pat) % loc is a vector of locations where string pat occurs in string dna pat_len = length(pat); dna_len = length(dna); index = 1; for(i = 1 : dna_len - pat_len + 1) % verifying a pattern matches found = 1; for(j = 1 : pat_len) if(dna(i+j-1) ~= pat(j)) found = 0; end end % if pattern matches we store location if(found == 1) loc(index) = i; index = index + 1; end end end % % 2 sample solution % function result = myDeterminant(x) % result is the determinant of matrix x. a1, a2, a3 are the minor matrices. a1(1,1) = x(2,2); a1(1,2) = x(2,3); a1(2,1) = x(3,2); a1(2,2) = x(3,3); a2(1,1) = x(2,1); a2(1,2) = x(2,3); a2(2,1) = x(3,1); a2(2,2) = x(3,3); a3(1,1) = x(2,1); a3(1,2) = x(2,2); a3(2,1) = x(3,1); a3(2,2) = x(3,2); result = x(1,1)*det(a1) - x(2,2)*det(a2) + x(3,3)*det(a3); end % % Alternate 2 sample solution % function output=myDeterminant(x) % output is the determinant of matrix x. output= x(1,1)*det(x(2:3,2:3)) ... - x(1,2)*det(x(2:3,[1 3])) ... + x(1,3)*det(x(2:3,1:2)); % % 3 sample solution % function minVal = minInNeighborhood(M, loc) % minVal is the lowest value in the neighborhood of position % (loc(1),loc(2)), i.e., M(loc(1)-1:loc(1)+1, loc(2)-1:loc(2)+1). % M is a matrix of numbers. loc is a vector of length 2. [nr, nc]= size(M); % dimensions of M % creating a bigger matrix which pads the borders for(i = 1:nr+2) for(j = 1:nc+2) if(i == 1 || j == 1 || i == nr+2 || j == nc+2) bigM(i,j) = realmax; else bigM(i,j) = M(i-1,j-1); end end end nr= nr+2; nc= nc+2; % dimensions of bigM r= loc(1)+1; c= loc(2)+1; % center of neighborhood in bigM % searching for the min value % minVal= minInMatrix( bigM(r-1:r+1, c-1:c+1) ); minVal = realmax; for(i = r-1: r+1) for(j = c-1 : c+1) if(minVal > bigM(i,j)) minVal = bigM(i,j); end end end end