Problem Statement: Write a script that finds a smaller user inputted matrix insi
ID: 3853479 • Letter: P
Question
Problem Statement: Write a script that finds a smaller user inputted matrix inside of a larger user inputted matrix. We will call this small user inputted matrix, the Waldo matrix, and the larger user inputted matrix the background matrix. Your script should check to make sure that neither dimension of the Waldo matrix is larger than the dimensions of the background matrix. If this is the case the script should repeat only the input for the Waldo Matrix until a new Waldo Matrix is entered whose dimensions are smaller than the background matrix. The program should then display the locations of where the Waldo matrices were found. (A matrix can have more than one Waldo in it.). (The location of a Waldo are the indexes of the top left element and the bottom right element in the background matrix where the Waldo’s occur.)
Coding Requirements: Allow for two user inputted matrices, the background matrix and the Waldo matrix. Warnings when the user inputs an improper “Waldo” matrix. You must use explicit loops to solve this problem, NO IMPLICIT LOOPS ARE ALLOWED. Your script must work for any size background matrix and any size Waldo matrix as long as they follow the rules in the problem statement.
This is done on the program MATLAB.
Explanation / Answer
function printWaldo(background, waldo)
[rowBackground colBackground] = size(background);
[rowWaldo colWaldo] = size(waldo);
for i = 1:rowBackground
for j = 1:colBackground
if background(i,j) == waldo(1,1) && i + rowWaldo - 1 <= rowBackground && j + colWaldo - 1 <= colBackground %if the first element is equal and
%size of waldo can fit in background matrix
%starting from (i,J)
flag = true; %flag determines if waldo cannot be found
for m = 1:rowWaldo
for n=1:colWaldo
if waldo(m,n) ~= background(i+m-1, j+n-1) %if one element does not match, we do not need to search more
flag = false;
break;
end
end
if flag == false %if flag is already false, stop searching
break
end
end
if flag %if flag is false, waldo not found
printf("Waldo found from (%d,%d) to (%d,%d) ",i,j, i+rowWaldo-1, j+colWaldo-1); %if the flag is still true, waldo found
end
end
end
end
end
function [background waldo] = inputWaldo() %normal input function... so am Not commenting
rowBackground = input('Enter number of rows in Background matrix: ');
colBackground = input('Enter number of columns in Background matrix: ');
background = ones(rowBackground, colBackground);
printf("Enter elements of background matrix ");
for i = 1:rowBackground
for j = 1:colBackground
printf("background(%d, %d)",i,j);
background(i, j) = input('=');
end
end
rowWaldo = input('Enter number of rows in Waldo matrix: ');
colWaldo = input('Enter number of columns in Waldo matrix: ');
while colBackground < colWaldo && rowBackground < rowWaldo %while loop determines waldo is smaller
printf("Waldo matrix should be less smaller than background matrix ");
rowWaldo = input('Enter number of rows in Waldo matrix: ');
colWaldo = input('Enter number of columns in Waldo matrix: ');
end
waldo = ones(rowWaldo, colWaldo);
printf("Enter elements of background matrix ");
for i = 1:rowWaldo
for j = 1:colWaldo
printf("waldo(%d, %d)",i,j);
waldo(i, j) = input('=');
end
end
end
[bg, w] = inputWaldo()
printWaldo(bg, w)
Am really sorry for the delay.. Had loads of question to solve... If you have any confusion or you want the functions to be merged into 1 function, let me know.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.