1. Matrix
manipulations
%RandArray.m
M = input('Enter M: ');
N = input('Enter N: ');
A = rand( M, N )
for ii = 1 : M
for jj = 1 : N
if A( ii, jj ) < 1/pi
A(
ii, jj ) = 0;
else
A(
ii, jj ) = 1;
end
end
end
A
%Alternate way without for loops:
%A( A < 1/pi ) = 0; A( A > 0 ) = 1;
2. Course scores
%Scores.m
marks = floor( 101*rand( 20, 3 ) );
grade = 0.2*marks(:, 1) + 0.2*marks( :, 2) + 0.6*marks( :, 3 )
3. Dominating permutations
Part A.
%Dominate.m
clear all;
n = input('Enter a size for the array: ');
nums = ceil( rand(1, n )*n );
isDom = 1;
for j = 2 : ( n - 1 )
if nums( j - 1 ) > nums( j )
& nums( j + 1) > nums( j )
isDom
= 0;
end
end
if isDom
disp( [ num2str( nums ) ' is dominating!' ] );
else
disp( [ num2str( nums ) ' is not dominating.' ] );
end
Part B.
%DominateFact.m
clear all;
n = input('Enter a value for n: ');
myperms = perms( 1 : n ); %assign to myperms a matrix containing all the
permutations of 1:n
count = 0; %number of dominating permutations
for i = 1 : length( myperms )
nums = myperms( i, : ); %assign nums to current row of
perms
isDom = 1;
for j = 2 : ( n - 1 )
if
nums( j - 1 ) > nums( j ) & nums( j + 1 ) > nums( j ) %not dominating
isDom
= 0;
end
end
%report results for current row
if isDom
count = count + 1;
disp( [ num2str( nums ) ' is
dominating!' ] );
end
end
disp(['The fraction which are dominating is ' num2str( count/length(myperms) )
] );
disp(['The theoretical result is is ' num2str( 2^(n-1)/prod(myperms(1, :) )) ]
);