type change8to9.m function [output] = change8to9( inputMatrix) %changes the first 8 in each row to a 9 in a matrix and returns that altered matrix %input: matrix %output: matrix lengths = size(inputMatrix); %get size of matrix and store in 1x2 array fprintf('size of the matrix m by n is ') disp(lengths) for m = 1:lengths(1) for n = 1:lengths(2) if (inputMatrix(m,n) == 8) %if that position equals an 8 inputMatrix(m,n) = 9; %change that position in the matrix to a 9 using an assignment statement break; %break out of this inner FOR loop end %ends the IF end %ends the inner FOR loop end %ends the outer FOR loop output = inputMatrix; %assign the matrix to the output value (or in other words, "return the (possibly) altered matrix") end %end the function %Note, this function changes the first 8 in each row to a 9. while only the first? matrixA = [1 2 3; 4 5 6; 7 8 9; 8 8 8 ; 8 1 1; 8 0 8] matrixA = 1 2 3 4 5 6 7 8 9 8 8 8 8 1 1 8 0 8 change8to9(matrixA) size of the matrix m by n is 6 3 ans = 1 2 3 4 5 6 7 9 9 9 8 8 9 1 1 9 0 8 %notice the first 8 in each row is now a 9 %if we look at matrixA again matrixA matrixA = 1 2 3 4 5 6 7 8 9 8 8 8 8 1 1 8 0 8 %it hasn't changed! %that's because we didn't assign anything to it and once it is copiied into the function, the function then alters the copy. not the original matrix %the following assignment statement would change it matrixA = change8to9(matrixA) size of the matrix m by n is 6 3 matrixA = 1 2 3 4 5 6 7 9 9 9 8 8 9 1 1 9 0 8 matrixA matrixA = 1 2 3 4 5 6 7 9 9 9 8 8 9 1 1 9 0 8 %and we can repeat this process and turn every 8 into a 9 matrixA = change8to9(matrixA) size of the matrix m by n is 6 3 matrixA = 1 2 3 4 5 6 7 9 9 9 9 8 9 1 1 9 0 9 matrixA = change8to9(matrixA) size of the matrix m by n is 6 3 matrixA = 1 2 3 4 5 6 7 9 9 9 9 9 9 1 1 9 0 9 matrixA matrixA = 1 2 3 4 5 6 7 9 9 9 9 9 9 1 1 9 0 9 diary off