Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function, named Three, which takes an argument a 3x3 matrix of integers

ID: 3563837 • Letter: W

Question

Write a function, named Three, which takes an argument a 3x3 matrix of integers and returns 1 iff the matrix

has a row, a column, or a diagonal that has the same integer value in its three slots. For example, Three([1 0

2; 5 5 5; 1 2 4]) should return 1 since each entry in the second row is 5. As another example, Three([4 2 7; 1

5 7; 8 9 7]) should return 1 since each entry in the third column is 7. As a third example, Three([2 1 4; 3 4 6;

4 7 8]) should return 1 since each entry in the other diagonal is 4.

Explanation / Answer

% Write a function, named Three, which takes an argument a 3x3 matrix of integers and returns 1 iff the matrix has a row, a column, or a diagonal that
% has the same integer value in its three slots. For example, Three([1 0 2; 5 5 5; 1 2 4]) should return 1 since each entry in the second row is 5.
% As another example, Three([4 2 7; 1 5 7; 8 9 7]) should return 1 since each entry in the third column is 7.
% As a third example, Three([2 1 4; 3 4 6; 4 7 8]) should return 1 since each entry in the other diagonal is 4.

function result = Three(v)
result = 0;
for i=1:3
if length(unique(v(:,i))) == 1
    result=1;
end
end
for i=1:3
   if length(unique(v(i,:))) == 1
   result=1;
   end
end
if length(unique(diag(v))) == 1
result=1;
end
if length(unique(diag(fliplr(v)))) == 1
result=1;
end
end

Three([1 0 2; 5 5 5; 1 2 4])
Three([4 2 7; 1 5 7; 8 9 7])
Three([2 1 4; 3 4 6; 4 7 8])
Three([1 2 3; 4 5 6; 7 8 9])

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote