oneByOneMatrix = 8 oneByOneMatrix = 8 twoByTwoMatrix = [8 8; 8 8] twoByTwoMatrix = 8 8 8 8 type changeOnlyOne8to9.m function [output] = changeOnlyOne8to9( inputMatrix) %changes only the first 8 to a 9 in a matrix and returns that altered matrix %(The matrix is traversed row by row.) %input: matrix %output: matrix lengths = size(inputMatrix); %get size of matrix and store in 1x2 array eightNotSeenYet = true; %initialize flag to true m = 1; %initialize m to 1 n = 1; %initialize n to 1 disp(lengths(1)); disp(lengths(2)); while(eightNotSeenYet && (m <= lengths(1))) n = 1; while(eightNotSeenYet && (n <= 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 eightNotSeenYet = false; %set flag to false fprintf('inside if: m and n are %d, %d\n', m, n); end %ends the IF fprintf('m and n are %d, %d\n', m, n); n = n + 1; end %ends the inner While loop m = m + 1; end %ends the outer While loop fprintf('eightnotseenyet, m, n, %d %d %d', eightNotSeenYet, m, n); output = inputMatrix; %assign the matrix to the output value (or in other words, "return the (possibly) altered matrix") end %end the function changeOnlyOne8to9.m(oneByOneMatrix) {??? Undefined variable "changeOnlyOne8to9" or class "changeOnlyOne8to9.m". } changeOnlyOne8to9(oneByOneMatrix) 1 1 inside if: m and n are 1, 1 m and n are 1, 1 eightnotseenyet, m, n, 0 2 2 ans = 9 changeOnlyOne8to9(twoByTwoMatrix) 2 2 inside if: m and n are 1, 1 m and n are 1, 1 eightnotseenyet, m, n, 0 2 2 ans = 9 8 8 8 %now, notice if we run it again, changeOnlyOne8to9(twoByTwoMatrix) 2 2 inside if: m and n are 1, 1 m and n are 1, 1 eightnotseenyet, m, n, 0 2 2 ans = 9 8 8 8 %the results are the same! Why? %because we didn't assign the returned matrix to anything. %Let's do that: newMatrix = changeOnlyOne8to9(twoByTwoMatrix) 2 2 inside if: m and n are 1, 1 m and n are 1, 1 eightnotseenyet, m, n, 0 2 2 newMatrix = 9 8 8 8 newMatrix newMatrix = 9 8 8 8 %now, we can input the newMatrix back in newMatrix = changeOnlyOne8to9(newMatrix) 2 2 m and n are 1, 1 inside if: m and n are 1, 2 m and n are 1, 2 eightnotseenyet, m, n, 0 2 3 newMatrix = 9 9 8 8 %notice two nines are in the matrix now %once more: newMatrix = changeOnlyOne8to9(newMatrix) 2 2 m and n are 1, 1 m and n are 1, 2 inside if: m and n are 2, 1 m and n are 2, 1 eightnotseenyet, m, n, 0 3 2 newMatrix = 9 9 9 8 %and again: newMatrix = changeOnlyOne8to9(newMatrix) 2 2 m and n are 1, 1 m and n are 1, 2 m and n are 2, 1 inside if: m and n are 2, 2 m and n are 2, 2 eightnotseenyet, m, n, 0 3 3 newMatrix = 9 9 9 9 %all the eights are now nines diary off