type count8sVector.m function output = count8sVector( inputVector) %count number of 8s in a one dimensional array (i.e. a vector) numberOfEights = 0; %set number of observed 8s to zero lengthOfInputVector = length(inputVector); %get length of the vector for k = 1:lengthOfInputVector %for k equals 1 to length of vector if (inputVector(k) == 8) %if the kth position of the vector equals 8... numberOfEights = numberOfEights + 1; %increment the number of observed 8s fprintf('k is %d ', k); %print out the position of this observed 8 end %end the IF end %end of FOR loop output = numberOfEights; %return the number of observed 8s end someVector = [8 0 8] someVector = 8 0 8 count8sVector(someVector) k is 1 k is 3 ans = 2 anotherVector = [8 7; 8 8] anotherVector = 8 7 8 8 count8sVector(anotherVector) k is 1 k is 2 ans = 2 type count8sMatrix.m function output = count8sMatrix( inputMatrix) %count number of 8s in a two dimensional array (i.e. a matrix) numberOfEights = 0; %set number of observed 8s to zero lengths = size(inputMatrix); %get m by n size of the matrix (m rows, n columns). And it these two dimensions are stored %in a 1 by 2 array called lengths fprintf('size of the matrix m by n is ') %print out... disp(lengths) %the dimensions of the matrix for m = 1:lengths(1) %for m equals 1 to "whatever is in the first index of lengths" for n = 1:lengths(2) %for n equals 1 to "whatever is in the second index of lengths" if (inputMatrix(m,n) == 8) %if the mth row and the nth column in the matrix is an eight, then... numberOfEights = numberOfEights + 1; % ...increment the number of observed eights end end %end of FOR loop for the columns end %end of FOR loop for the rows output = numberOfEights; %output the number of observed eights end someMatrix = [8 8 8; 8 0 8] someMatrix = 8 8 8 8 0 8 count8sMatrix(someMatrix) size of the matrix m by n is 2 3 ans = 5 diary off