% Moed 2010 B bio MatlabBio 76623 6/9/2010 %% part A solution clc clear format compact %% 1 A = [linspace(2,11,4) ; 12 -7 5 9; ones(1,4)*7] B = sort(A) C = B(end,:) %%2 D = [1 -1 3;7 5 -3;-1 3 2; 7 6 5] M = logical(D+1) Z = reshape(M,2,6) %%3 R = [6 3 ; 3 7 ; 11 9 ; 20 30] s = min(R,[],2) u = numel(s) %%4 a = 6:10 b = fliplr(a) b(7) = 15 x = b>=8 & mod(b,3)==0 %%5 v=5:-2:1 q= v*(1:3)' g = q*eye(2) %% Solution %{ %% 1 A = 2 5 8 11 12 3 5 9 7 7 7 7 B = 2 3 5 7 7 5 7 9 12 7 8 11 C = 12 7 8 11 %% 2 D = 1 -1 3 7 5 -3 -1 3 2 7 6 5 M = 1 0 1 1 1 1 0 1 1 1 1 1 Z = 1 0 0 1 1 1 1 1 1 1 1 1 %%3 R = 6 3 3 7 11 9 20 30 s = 3 3 9 20 u = 4 %%4 a = 6 7 8 9 10 b = 10 9 8 7 6 b = 10 9 8 7 6 0 15 x = 0 1 0 0 0 0 1 %%5 v = 5 3 1 q = 14 g = 14 0 0 14 %} ------------------------------------------------- function [m r] = q1(K ) %function Q1 receives matrix K and returns: % the max value in m % the short index of the places in which the max value appears in r m=0; m=max(max(K)); % Short way r=find(K==m); % Long Way r=[]; for i = 1:numel(K) if K(i)==m r=[r i]; end end end ---------------------------------------------------- function [L] = q2(M ) %function Q2 receives matrix M and returns: % True if all rows and columns in matrix are sorted in ascending order, % false otherwise % Short way %{ S1=[]; S2=[]; S1=sort(M); S2=sort(M,2); if M==S1 & M==S2 L=true else L=false end %} % long way [x y] = size(M); L=true for i=1:x for j=1:y-1 if M(i,j) > M(i,j+1) L = false return end end end for j=1:y for i=1:x-1 if M(i,j) > M(i+1,j) L = false return end end end end --------------------------------------------------- function u = q3(D) % 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 = ones(m,1); for x = 1:m for y = 1:n if D(x,y) ==0 u(x) =0; end end end Same as : all(D,2)